Exception handling mechanism exists mainly for the program in the run, if there is an exception, the program can be uninterrupted, and continue to run.
Exceptions are classified as run-time exceptions, and non-runtime exceptions.
If it is a run-time exception, we usually do not handle it.
Rather than exceptions, we handle them in several ways.
-----------------------------------
If you are dealing with exceptions in this method, we usually use a try statement block to handle
The content in the Try statement is the lookup exception, and if an exception is found, the subsequent catch statement
Capture, and make the processing you want, a try statement can have a lot of catch statements
However, because exceptions are mostly inherited from the parent class, catch multiple exceptions when
The exception class must be large to small, because if the parent class is in front, an exception occurs
Will be captured by the first parent class, and subsequent catch will not allow
This is a little similar to the parent class reference of the subclass object.
Finally statement
This statement must be executed, whether or not an exception occurs.
Add:
If there is a return statement in the try Block
His role must be to execute the contents of the Finally block first
will be implemented, and the statement after finally will not be implemented
------------------------------------
Usually, we encounter anomalies like this.
Exceptions may occur in a method
Call a method in the B method
At this point, we generally throw exceptions in the A method.
Then capture in the B method (that is, use a try block)
A method throws an exception by throws the exception class after the method name (equivalent to declaring it)
Then throw the new exception class in the method.
So the A method throws the exception and then the B method can catch
And if we continue behind the main method throws
This time the exception continues, while the virtual machine is processed.
Java exception (Novice beginner)