1. many new users don't want to read the exception when they encounter Exception Handling. They don't think it is necessary. In fact, they just need a few lines of code to understand the benefits of exception handling. Naturally, they don't want to read it, but learning is not a sloppy thing !! So why do we need to handle exceptions ?? Early programming languages (such as the C language) do not handle exceptions. Generally, a special value is returned or a flag is set in case of an error to determine whether an error has occurred. As the system grows, such error handling has become an obstacle for creating large maintenance programs. Therefore, exception handling mechanisms are introduced in some languages, such as the "on error goto" Exception Handling statement in basic, while Java creates a new exception handling mechanism based on C ++.
2. Exception Handling in Java uses object-oriented methods to handle exceptions, classify various exceptions, and provide good interfaces. This mechanism provides a powerful way to control complex programs. At the same time, these exception codes are separated from the "regular" code, enhancing the readability of the program and making the program more flexible.
Another advantage of exception handling is that you can submit the problem without determining or handling the exception. On the other hand, the exception handling mechanism makes the error handling code more organized and easier to maintain 3. java exception handling class in Java, each exception is an object, which is an instance of the throwable class or other sub-classes. When a method encounters an exception, an exception object is thrown. The object contains the exception information. The method that calls this object can capture the exception and handle it.
The throwable class has two standard subclasses: Java. Lang. Error and Java. Lang. exception, that is, errors and exceptions. Error errors generally refer to problems related to virtual machines, such as system crashes, virtual machine errors, and dynamic link failures. Such errors cannot be recovered or cannot be captured, resulting in application interruption. Exception exception class refers to some exceptions that can be captured and may be recovered, such as array subscript out of bounds arrayindexoutofboundsexcepton, number division by zero, arithmeticexception, input/output exception ioexception, etc.
Tip: the Java compiler requires the Java program to capture or declare all non-runtime exceptions, such as filenotfoundexception and ioexception. For such exceptions, unexpected results may occur if the program does not process them. However, exceptions may not be processed during running, because these exceptions are common and all processing may affect the readability and Running Efficiency of the program. two Methods for exception handling in Java: (1 ). use try... catch keyword; try {
// Code that might generate exceptions
} Catch (type1 id1 ){
// Handle exceptions of type1
} Catch (type2 Id2 ){
// Handle exceptions of type2
} Catch (type3 ID3 ){
// Handle exceptions of type3
} Finally
{....} Note: the code in the Finally block must be executed. No matter how the exception is handled, you can use the keyword finally to mark a piece of code that is executed no matter what exception occurs. A member function returns a member function that calls it, or uses an uncaught exception or a clear return statement, the finally Clause always happens to be executed before the member function returns (2 ). use the throw and throws keywords