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 of "how to use Java try and catch". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Although the default exception handlers provided by the Java runtime system are useful for debugging, you usually want to handle exceptions yourself.
First, it allows you to correct your mistakes. Second, it prevents the program from terminating automatically. Most users are annoyed (to say the least) about printing stack tracks whenever an error occurs when the program terminates.
Fortunately, this is easy to avoid.
To prevent and handle a run-time error, just put the code you want to monitor into a try block. The try block is followed by a catch clause that describes the type of error you want to catch. To accomplish this task, the following program contains a try block that handles ArithmeticException exceptions caused by division by zero and a catch clause.
Note that the call to println () in the try block is never executed. Once an exception is thrown, program control is transferred from the try block to the catch block. Execution never "returns" from the catch block to the try block. Therefore, "This will not be printed."
Will not be displayed. Once the catch statement is executed, program control continues from the following line of the entire try/catch mechanism.
A try and its catch statement form a unit. The scope of the catch clause is limited to the statements defined before the try statement. One catch statement cannot catch an exception thrown by another try declaration (except in the case of a nested try statement).
Statement declarations protected by try must be in curly braces (that is, they must be in a block). You can't use try alone.
The purpose of constructing the catch clause is to resolve the exception and continue to run as if an error had not occurred. For example, in the following program, each for loop gets two random integers repeatedly. These two integers are divided by each other, and the result is to divide 12345. The final result is in a. If a division operation causes a division by zero error, it will be caught, the value of an is set to zero, and the program continues.
Displays a description of an exception
Throwable overloads the toString () method (defined by Object), so it returns a string containing the description of the exception. You can display the description of the exception by passing a parameter to the exception in println (). For example, the catch block of the previous program can be rewritten as
That's all for "how to use Java try and catch". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.