What is an exception?
An exception refers to an exception event caused by an external problem during the program running. The exception will interrupt the program running. In Java, an exception is itself an object, and generating an exception is generating an exception object.
Three reasons for exceptions in Java
- Java internal error An exception occurs. The exception is generated on the Java virtual machine.
- An exception produced by an error in a program written, such as an out-of-bounds exception of a number group, a null pointer exception, etc. Such exceptions are known as run-time exceptions, or unchecked exceptions, and are not sensed at compile time.
- The exception that is manually generated by the throw statement, called the checked exception, is used to inform the caller of the method of some necessary information.
Exception handling mechanism in Java
- Catch exceptions
- An exception is thrown
Exception classes in Java
All exception classes in Java are subclasses of the Java.lang.Throwable class.Throwable is at the top level of the exception class. The Throwable class has two branches under Exception and Error, and two branches under Exception (RuntimeException, CheckedException).
- Exception: Exceptions that can be handled by a program, caught, and possibly recovered
- RuntimeException (RuntimeException) : Error, RuntimeException, and subclasses are unchecked exceptions. The Java compiler does not notice or detect such exceptions at compile time, and does not require the programmer to handle them in the program. For these exceptions, we should fix the code, not handle them through exceptions.
- CheckedException: The Java compiler forces the program to catch this exception, either by using try{}catch{} or by using the throws declaration after the method, otherwise it will not be compiled
- Error: Error indicates a serious problem that the application itself cannot overcome or recover from, such as memory overflow and thread deadlock.
Throws and throws
- Different position. Throws throws are used in the method body, followed by the exception object
- Different functions. Throws throws are used to declare exceptions, so that the caller only knows about the possible problems in the method. This lets the caller know that the exception needs to be caught when using the method, but does not handle it himself. A throw is used to throw a specific problem object. When executed, the function is finished, jumps to the caller, and throws the specific problem object to the caller. Throws indicates a possibility that exceptions will occur, not necessarily; A throw throws an exception. To execute a throw, you must throw some kind of exception object.
throws: public int div (int i,int j) throws ArithmeticException{ int temp = i/j; return temp; } public static void main(String[] args) {try{div(1,0); }catch(ArithmeticException e){ System.out.println(e); } } throw: public static void main(String[] args) { int i = 1; int j = 0; try{ if(j==0) throw new ArithmeticException(); }catch(ArithmeticException e){system.out.println (e); }}Copy the code
Custom exception classes
When writing your own exception class for custom exceptions, there are a few things to note:
- All exception classes must be subclasses of Throwable.
- If you want to customize a CheckedException, you need to extend the CheckedException class
- If you want to customize a RuntimeException, you need to extend the RuntimeException class
Finally, Finalize, final
- Finally: Finally is a part of a try{}catch{}finally{} block. The code in the finally{} block must be executed (if there is no return statement in the preceding try{} or catch{} block). It is usually used to release resources.
- Finalize: Finalize is a method in an Object whose Finalize () method is called by the garbage collector before the Object is deleted
- Final: is a modifier. A class that is final cannot be inherited, a method that is final cannot be overridden, and a variable that is final cannot be modified after it is initialized (not necessarily).