Abnormal
An exception is a flow of instruction that causes a program to break execution. In Java, there is also a clear definition of exceptions in the API, called exception classes.
- ERROR:JVM error, no processing in the program, to the virtual machine.
- Exception: An exception that occurs in a program that can be processed in a program. Exception is also divided into compile-time exceptions, run-time exceptions.
- Compile-time exception: compilation fails after code is written. Example: ParseException (Conversion exception), ClassCastException type conversion exception
- Run-time exception: the code itself is not a problem, can be compiled, run time error. For example: NullPointerException (null pointer exception), ArithmeticException (Math exception, 0 divisor), ArrayIndexOutOfBoundsException (array corner bounds exception)
Exception handling
1. Catch exception: Try catch directly handles possible exceptions 2. Declaring exceptions: The throws declaration tells a possible exception in the call, exposes the problem, and the caller handles the
try{ //可能出现异常的代码 }catch(Exception1 e1){ //处理的方式1 e.printStaticTrace(); }[catch](Exception2 e2{ //处理的方式2}[finally]{ //一定要执行的代码 }
- Finally: abnormal consent exits, regardless of whether the program produces an exception, the contents of the finally statement block are bound to be executed.
- Throws keyword: Used to declare a method, which means that this method does not handle the exception, but instead throws it to the call, who calls the method, and who handles the exception.
- Throw keyword: Normally, exceptions are thrown by the JVM, and in addition to this, we can throw an exception artificially by the Throw keyword.
Several ways to combine try,catch,finally:
- Try,catch,finally
- Try,catch
- Try,finally
Go Java exceptions