A small summary of Java exceptions

1. There are two subclasses of Error and Exception in Throwable

1.Error: There is an internal error in the system or the resource is exhausted (such as insufficient disk memory), and it is not enough to modify the program. we don't care

2.Exception: RuntimeException runtime exception, we need to fix the code

   Non-RuntimeException compile-time exception, which must be handled, otherwise the program will fail to compile

2. Several keywords

1.try...catch: The specific usage is   

 
 
try{
  // program code block
}catch(Exception e){
   // handle exception information
}
The two words cannot appear alone and must be placed together

2.finally

A: finally is used to release resources, and his code will always be executed. Special circumstances encounter System.exit(0); jvm exits

B: If there is a return in the catch, will finally be executed? If it is executed, before or after the return.

Yes, before.

3.throw和throws

Throw:
In the method body, followed by the exception object name, and only one
throw throws an exception object, indicating that there must be an exception that generated
throws:
On the method declaration, followed by an exception class name, can be multiple

Throws is to declare that the method has an exception. It is a possibility. This exception may not be generated.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325369051&siteId=291194637