Location>code7788 >text

Exception try-catch-finally with storage and

Popularity:728 ℃/2025-01-21 09:08:48

catch exception

Catching exceptions: Handling possible exceptions. When an error occurs, we handle it to prevent the program from crashing.

Exception handling try-catch-finally

try{
     // Possible exception: code 1
 }catch(err){
     // When an exception occurs in the code, this will be executed, and the exception object will be passed to err
     // If no exception occurs in the code, this will not be executed.
     // code 2
     ('Exception object', err)
 } finally{
     // Can be omitted, this will be executed regardless of whether an exception occurs.
     // code 3
     ('finally')
 }

Use try-catch to handle common exceptions [ctcm upgrade]

let obj = {}
 try{
   // Because an error may be reported.  The value of obj may be an array, object, or null
   obj = (('aa'))
   // Be careful when operating this obj, because it may be null
 }catch(err){
   //What to do after reporting an error
   obj ={}
 }
 ('obj', obj)

Throw exception manually

Not only will the browser automatically throw exceptions for us, we can also throw exceptions manually.
The syntax is throw exception object [this exception object can be a number, array, object]
But for the sake of standardization, we usually throw new error type
The following are common error types
throw new SyntaxError("SyntaxError")
throw new TypeError("TypeError")

Manually throwing exception errors

try{
     ("{name:123}")
 }catch(err){
     throw new SyntaxError('Syntax error')
 }

What are the error types?

1,SyntaxError means syntax error
2,TypeError type error
3, ReferenceError reference error
4,RangeError range error
5,URIErrorURI error
These error objects all inherit Error [Important]

TypeError type error

let numberValue = 1
 // Report error Uncaught TypeError[type error]
 (item => {
     ('item', item)
 })

syntax error

// VM41:1 Uncaught SyntaxError [indicates syntax error]
 ("{name:123}")

ReferenceError reference error

try {
     (str);
 } catch (err) {
     ('Reference error caught:', err);
 }

RangeError range error

try {
     let numArray = new Array(-1);
 } catch (err) {
     ('Caught range error:', err);
 }

URIError

try {
     decodeURIComponent('%');
 } catch (err) {
     ('URI error caught:', err);
 }

When an exception is thrown, the following code will not be executed.

try{
     ("{name:123}")
 }catch(err){
     throw new SyntaxError('Syntax error')
     ('1111')
 }
 ('222')

Will it output 111 and 222?
Answer: No. Because an exception is thrown, the following code will not be executed.

The code after the exception is thrown will not be executed, but finally will be executed
try{
     ("{name:123}")
 }catch(err){
     throw new SyntaxError('Syntax error')
     ('1111')
 } finally{
     ('finally')
 }
 ('222')

The output is finally

What is the output of localStorage and sessionStorage when getting a key that does not exist locally? [Tips]

Answer: The output is null,

let str = ('aa')
 ('str', str) // output null
 let arr = ('bb')
 ('arr', arr) // output null

What do you get by converting null? [Tips]

Answer: The result is also null

What does converting '[]' get? [Tips]

('obj', ('[]')) // The output is []

What does converting '{}' get? [Tips]

('obj', ('{}')) // The output is {}

What do you get by converting ""?

('obj', ('""'))//The output is ""empty string

What does converting '123' get? [Tips]

('str', ('1212')) // The output is 123

What is the output of this? [Tips]

('obj', ("{a:123}"))
An error will be reported because it is not in JSON format.

For local storage, should you consider using try-catch? [Tips]

Generally speaking, it is best to write

For local storage, should we consider the situation where a user has multiple accounts? [ctcm upgrade]

For compatibility issues, null, undefined, NaN, 0, '', '0' are usually considered