Java Exception Handling Mechanism
Background:
When using some java class libraries, you will find that when using some class libraries, you must add the try-catch or throw keywords.
Abstract The problem:
Problem Abstraction:
1. The author of the class library can detect the problem that occurs during running, but does not know how to handle the problem.
2. the user who uses this class library knows how to solve this problem, but does not know how to detect them.
Solution:
Exception is used to solve this problem.
1. If a method cannot determine how to handle the problem that occurs in an abnormal process in the program, it can throw the problem to the caller.
2. If the caller is unwilling to handle the problem, he or she can continue to throw it to the next caller, or if he or she is willing to handle the problem, he or she will receive the catch
And then handle the problem.
Java exception hierarchy
1 Note: The exceptions we are concerned about refer to non-error and runtime exceptions, that is, checkedException (exceptions that can be detected by the compiler)
2 error usually refers to an error that cannot be recovered, such as exceeding the upper limit of system performance. This error is thrown by the system.
3. RuntimeException is caused by a programmer's code problem, such as a null pointer.
Specific operations:
In java, checkedException is an object.
Therefore, when an exception is thrown, you must first select an appropriate exception type and then generate an instance to throw
try { // code } catch (ExceptionType e) { // exception handling code }
Running process:
When the code encounters an exception, it will ignore the subsequent code and directly jump to its corresponding catch to run
Complete instance:
Exception Handling except 0
public class JavaExceptionTest { public static void main(String[] args) { // TODO Auto-generated method stub try { double a=1/0; } catch(ArithmeticException e) { System.out.println(e.getMessage()); } }}
Java exception category:
Http://www.tutorialspoint.com/java/java_builtin_exceptions.htm
Java exception Teaching Document
Http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html