Exception handling: The exception handling mechanism in Java relies primarily on the try,catch,finally,throw,throws five keywords. Where the Try keyword is immediately followed by a block of code enclosed in curly braces (the curly braces must not be omitted) short as a try block. Place the code where the exception might occur. A catch corresponds to an exception type and a code block that indicates that the catch block is used to handle this type of code block. Multiple catch blocks can also be followed by a finally block. Finally blocks are used to reclaim the physical resources that are opened in the try block, and the exception mechanism guarantees that the finally block is always executed. The throws keyword is primarily used in method signatures to declare that the method may throw an exception thrown to throw an actual exception, and throw can be used as a statement, throwing a specific exception object. Java exceptions fall into two categories: checked exceptions and runtime exceptions. Java thinks that checked exceptions are exceptions that can be handled at compile time, so it forces the program to handle all checked exceptions, while runtime exceptions do not need to be handled. Checked exceptions can remind programmers to handle all possible exceptions, but checked exceptions also bring some complexity. For the error handling mechanism, there are two main disadvantages: 1. Unable to lift all abnormal situations: because the human knowledge is limited, the abnormal situation is always more than the situation that can be taken into account, there is always a slip through 2. Error handling code and business implementation code mix seriously affect the readability of the program, will increase the difficulty of program maintenance. Java exception Handling mechanism: the implementation of the "Business function code" and "error handling code" separation! 1. Using Try...catch to catch exceptions Java presents a hypothesis that if the program can be completed smoothly, then everything is normal, the system's business implementation code is defined in the try block, and all the exception handling logic is processed in the catch block. That
try{//Business implementation Code ...} catch (Exception e)
{Enter illegal} in the format above the try block and the catch block after {...} are not to be omitted! Steps: 1. If an exception occurs when executing the business logic code in a try block, the system automatically generates an exception object that is submitted to the Java runtime, which is called throw (throw) exception. 2. When the Java runtime receives an exception object, it looks for a catch block that can handle the exception object, and if a suitable cathc block is found and the exception object is handed over to the catch block, the process is called catch (catch) exception If the Java Runtime Environment cannot find a catch block to catch the exception, the runtime environment terminates and the JAV program exits.
When making an exception capture, it is important to remember to catch small exceptions before capturing large exceptions.
Access exception information:
If the program needs to access information about the exception object in the catch block, it can be obtained by calling the method of the exception parameter after the catch.
When the Java runtime decides to call a catch block to process the exception object, the exception object is assigned to the catch
The exception parameter after the block, which the program can use to obtain information about the exception.
All exception objects contain the following common methods:
GetMessage (): Returns the detailed description string for the exception
About Java exception handling