Development with try-catch in JavaScript

After learning the try-catch construct, your coding style should change. Now you must wrap all places where an exception may occur in a try-block, and write a reaction to this exception in a catch-block.

Задание 1

Given the following code:

javascriptlet str = 'some string';
localStorage.setItem('key', str);
alert('saved successfully!');

What's wrong with this code? Fix the shortcomings of this code.

Задание 2

Given the following code:

javascriptlet json = '[1,2,3,4,5]';

let arr = JSON.parse(json);

let sum = 0;
for (let elem of arr) {
  sum += +elem;
}

alert(sum);

What's wrong with this code? Fix the shortcomings of this code.