In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the exception handling mechanism of C++, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.
When a program goes wrong, there are three possible situations: syntax errors, run-time errors, and logic errors. Syntax error means that the format of the code is wrong, or a letter is typed incorrectly; runtime error refers to some unexpected errors that occur when the program is running, such as null pointer exception, array out of bounds, divisor zero, etc. Logic error means that the running result is different from the expected result, which is a kind of error that is difficult to debug. The exception handling mechanism in java mainly refers to handling run-time errors, that is, exceptions are run-time errors.
There are three reasons for the exception: an error occurred within 1.java and an exception generated by the java virtual machine. two。 Exceptions caused by errors when writing a program, such as null pointer exception, array out of bounds, etc. 3. The exception generated by the throw statement. This exception is often referred to as a "check exception" and is used to inform the call to the method of relevant information.
Java handles exceptions through an object-oriented approach. If an exception occurs during the operation of a method, the method produces an object that represents the exception and gives it to the runtime system, which looks for the corresponding code to handle the exception. The process of generating an exception object and handing it over to the runtime system is called throw. The process by which the runtime system searches the call stack of a method until it finds an object that can handle the exception is called a catch.
I. Classification of anomalies
In java, any exception belongs to the java.lang.Throwable class or its subclasses, and the Throwable class is divided into Exception class and Error class. The exception class is used for all kinds of exceptions that occur by the user, and it can also be used to create classes of custom types. Error class exceptions refer to errors related to the running of java programs that are related to the system itself. These exceptions are usually catastrophic and cannot be controlled by the program.
Exception class exceptions are divided into run-time exceptions and non-run-time exceptions. Run-time exceptions refer to the RuntimeException class and its subclasses. These exceptions do not check for exceptions, and you can choose to capture and handle them. Non-run-time errors are exceptions other than the RuntimeException class that must be handled or the program will fail to compile.
2. Exception handling mechanism
There are two main ways to handle exceptions: positive handling and negative handling.
The positive way to deal with it is to use try...catch. The program first executes the statement in try. When the statement in try is abnormal, it stops the execution of the current program and transfers it to catch to execute the statement. If there is an exception in the catch statement, the program will stop execution, and then go to the next program of catch to continue execution. That is, try statements are used to find exceptions, while catch statements are used to handle exceptions. The catch statement is usually followed by a final statement, which is used in conjunction with the try...catch statement, and the statements in the final will be in a straight line regardless of whether there is an exception in the try and catch. If a method must return a value, then it is most appropriate to put the return statement in final, because if you put it in either try or catch, if an exception occurs during the run, the program will stop executing and the retrun will not be executed.
The negative approach refers to the implementation of throw and throws. Throw actively throws an exception and terminates the program. Throws is used after the method to declare an exception that tells the consumer of the method which exceptions occurred in the method, allowing the user to handle these exceptions according to his or her own situation.
1.try...catch statement format
The parameters of the catch statement include an exception type and an exception object, which must be a subclass of Throwable, indicating the types of exceptions that the catch statement can handle. There can be multiple catch statements that handle different types of exceptions. A catch statement can also catch multiple exception types, and in this case, the exception type parameter of catch should be the parent of these exception types.
Here is an example of the use of the try...catch statement:
The e here represents the exception object, the cause of the exception can be obtained by the toString () method, and the cause and location of the exception can be obtained by the printStackTrace () method.
two。 Multiple catch statements
When an exception may occur in a block, you can use multiple catch statements, each catch statement catches an exception, and when an exception is thrown, each catch checks it once. When * catch statements are executed, other catch statements are not executed, but when the catch execution ends, the next catch statement is executed one at a time.
It is important to note that when using multiple catch statements, catch statement blocks should be divided in order. If the parent exception precedes the subclass exception, the parent exception is executed first, and the subclass exception will never be executed, so that an error occurs while the program is running.
3.final statement
As mentioned earlier, try statements are executed regardless of the exceptions that occur in final and catch statements. There are three situations in which final is performed:
(1) if there is no exception in the try statement, the final statement is executed after the try statement is executed, followed by the subsequent statement.
(2) if there is an exception in the try statement and the exception thrown is caught by catch, the execution of the program will stop where the exception occurs and execute the matching catch statement instead. * execute the final statement. If there is no exception in the final statement, continue to execute the following statement. If there is an exception in the final statement, pass the exception to the caller of the method.
(3) if the exception thrown in the try statement is not caught by the catch statement, the final statement is executed directly and the exception is passed to the caller of the method.
III. Throwing and declaring exceptions
1. Throw an exception
You can use the throw statement to throw an exception, which is in the basic form
Where ThrowableInstance must be the Throwable class or a subclass of it
two。 Declare exception
If a method can cause an exception but does not handle it, you can use the throws statement to declare the exception in the basic form:
A throws sentence lists all the types of exceptions that may occur in a method, separated by commas. The exceptions of the Error and RuntimeException subclasses do not need to be specified in the list of exceptions, while other types of exceptions must be specified, or errors will occur at compile time.
IV. Custom exception
Java built-in exceptions can handle most common run-time errors, but you can also define them yourself, which are usually obtained by overloading the Exception constructor. For example:
Thank you for reading this article carefully. I hope the article "what is the exception handling mechanism of C++" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.