How the JVM handles exceptions by default
When the main function receives this problem, there are two ways to handle it:
- Handle the problem yourself, and then continue to run
- There is no way to deal with it, only the JVM that calls main to handle
The JVM has a default exception handling mechanism that handles the exception. and the name of the exception, the exception information. The location where the exception occurred is printed on the console, and the program stops running
Exception handling Mode try...catch...finally
Try: Used to detect exceptions
Catch: Used to catch exceptions
Finally: freeing resources
P style= "margin:0px 0px 1.2em!important" > When the problem is handled by Trycatch, the program will continue to execute
- try catch
//notation 1 int a = 10; int b = 0; Int[] arr = {11,22,33,44,55}; try {System.out.println (A/b); System.out.println (arr[10]); arr = null; System.out.println (Arr[0]); } catch (ArithmeticException e) {System.out.println ("divisor cannot be zero"); } catch (ArrayIndexOutOfBoundsException e) {System.out.println ("Index Out of Bounds"); } catch (Exception e) {//exception e = new NullPointerException (); SYSTEM.OUT.PRINTLN ("error"); }
//写法2//JDK7如何处理多个异常 try { System.out.println(a / b); System.out.println(arr[10]); } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println("出错了"); }
Multiple catch, can only catch an exception, try after if with more than one catch, then small exception put in front, large exception put behind, according to the principle of polymorphism, if the large put in front, will all the sub-class objects receive, after the catch is meaningless
- Try Catch finally
- Try finally
The difference between throws and throw throws
- Used after the method declaration, followed by the exception class name
- Can be separated by a comma with multiple exception class names
- Represents a thrown exception that is handled by the caller of the method
Throw
- Used in the body of a method, followed by the name of the exception object
- Only one exception object name can be thrown
- Represents a thrown exception, handled by a statement in the method body
Features of finally keyword-specific and function-finally
The statement body that is finally controlled must be in the try Catch Finally in the execution.
Special case: The JVM exits before execution to finally (e.g. System.exit (0))
The role of finally
Used to free up resources, and in IO stream operations and database operations, you will see
The difference between final,finally and finalize: The relationship between Lei Feng and Leifeng Tower
Final can be decorated with classes, cannot be inherited, decorated methods, cannot be overridden, modifier variables, can only be assigned one time
Finally is a statement body in a try statement and cannot be used alone to free resources
Finalize is a method that is called by the object's garbage collector when the garbage collector determines that no more references to the object exist.
Custom exceptions
Why do I need a custom exception?
By the name of the end is the god horse anomaly, there are targeted solutions, such as: The Age of people
class AgeOutOfBoundsException extends Exception { public AgeOutOfBoundsException() { super(); } public AgeOutOfBoundsException(String message) { super(message); }}
Exception considerations:
- When a subclass overrides a parent class method, the child class's method must throw the same exception or subclass of the parent exception. (father is broken, son can not be worse than father)
- If the parent class throws more than one exception, when the subclass overrides the parent class, it can only throw the same exception or a subset of his, the subclass cannot throw exceptions that the parent class does not have
- If the overridden method does not throw an exception, then the method of the subclass is absolutely not allowed to throw an exception, and if an exception occurs within the subclass method, then the subclass can only try, not throws
?
Exception Handling in Java