In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge about "how to use JavaScript exception handling technology". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to handle these situations! I hope you can read carefully and learn something!
What is an exception?
The Javascript interpreter raises exceptions when Javascript programs run with problems such as array index out-of-bounds, type mismatches, or syntax errors. ECMAscript defines six types of errors, in addition to which we can use Error objects and throw statements to create and raise custom exception handling messages.
II. Advantages of exception handling techniques
By using exception handling technology, we can respond to error events in a structured way, separate exception handling code from normal script code scientifically, and finally focus on writing core programs that complete major functions.
Try…catch…finally.
In Javascript, we use the try…catch…finally statement to perform exception handling, i.e., to catch exceptions caused by errors or throw statements. Its basic syntax is as follows:
try {
//Here are the statements that may produce exceptions
} catch(error) {
//Here is the statement responsible for exception handling
} finally {
//This is the exit statement
}
In the above code, the statements in the try block are executed first. if an error occur during that run, control is transfer to the statement in the catch block, where the error parameter in parentheses is passed as an exception variable. Otherwise, catch block statements are skipped and not executed. Whether the statements in the catch block finish executing when an error occurs, or the statements in the try block finish executing when no error occurs, the statements in the finally block will eventually be executed.
Here's an example:
We input abc, then determine, and the output is as follows:
"Start executing try block statement---> No exceptions have occurred yet---> Exception caught, start executing catch block statement---> Error name: TypeError ---> Error message: 'abc' undefined---> Start executing finally block statement"
The above routine starts with a try block statement. When we output the message "No exceptions have occurred yet," an input dialog box pops up asking the user to enter a value. When we enter the illegal message "abc," an exception is raised, so the remaining statements in the try block will be skipped and the catch block statement will be executed. The err parameter at the beginning of the Catch block serves as the error object for this exception, which has two attributes: name and message. Finally, execute the finally block statement.
We see that since no errors occurred, when the statements of the try block were executed, the catch block statements were skipped, a window appeared showing the values entered, and finally the statements of the finally block were executed.
4. Try... catch... finally deformed
The try…catch…finally statement has two variants, try…catch or try…finally.
Try…catch is the most common structure, and its execution process is: When no exceptions occur after the completion of the try block statement or an exception occurs after the completion of the catch block statement, control will be transferred to the entire try…catch structure after the statement. Consider the following example:
try {
[xss_clean]ln("Beginnng the try block")
[xss_clean]ln("No exceptions yet")
// Create a syntax error
eval("6 + * 3")
[xss_clean]ln("Finished the try block with no exceptions")
} catch(err) {
[xss_clean]ln("Exception caught, executing the catch block")
[xss_clean]ln("Error name: " + err.name)
[xss_clean]ln("Error message: " + err.message)
}
[xss_clean]ln("Executing after the try-catch statement")
If it is a try…finally construct, then when an exception occurs, the finally block statement is not executed because there is no catch block statement to catch the error. Therefore, this structure is rare in practical applications.
5. The form of exception: Error object
In Javascript, exceptions are those that appear as Error objects. The Error object has two attributes: the name attribute indicates the type of exception, and the message attribute indicates the meaning of the exception. Depending on the values of these attributes, we can decide how to handle exceptions, such as:
function evalText() {
try {
alert(eval(prompt("Enter Javascript to evaluate:","")))
} catch(err) {
if(err.name == "SyntaxError") alert("Invalid expression")
else alert("Cannot evaluate")
}
}
The code above evaluates the expression to what the user enters and displays it. If a SyntaxErroe type error occurs during evaluation, the user is presented with the message "Invalid expression"; otherwise, the user gets the message "Cannot evaluate."
Error.name has six values, as follows:
EvalError: The use of eval() is inconsistent with the definition
RangeError: Value out of bounds
ReferenceError: Illegal or unrecognized reference value
SyntaxError: A syntax parsing error occurred
TypeError: Wrong operand type
URIError: URI Handling Function Misused
"How to use JavaScript exception handling technology" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.