There are two ways to handle Java exceptions
1. Catching exceptions
2. Transfer exceptions
First of all, the catch exception (for a system, exception handling is very critical, well handled to avoid and reduce system bugs, but also very good to locate the source of the problem)
Catch exceptions are handled by the try-catch-finally structure, typically in two formats
As shown below:
struct 2try {//May produce an exception to the statement} catch (Exception e) {//catch to Exception after processing statement}//structure 2try {//statement that may produce an exception} catch (Exception e) {//Snap to exception after processing statement} finally {}
Note: Catch and finally do not necessarily exist at the same time. But the try must exist in conjunction with one of the two, meaning that the structure can be try-catch and try-finally
Write an example of a simple exception capture
public class Exceptiontest {public static void main (string[] args) {try {//dividend cannot be 0, program throws exception int i = 0, j = 1;int k = j/i;} catch (Exception e) {System.out.println ("Caught to Exception"); E.printstacktrace ();}}
If the denominator on I cannot be 0, the statement in the try program throws an exception, and the catch catches the exception and executes the statement in the catch
The results of the operation are as follows:
Caught exception Java.lang.ArithmeticException:/by Zeroat Exceptiontest.main (Exceptiontest.java:7)
When is the catch going to be executed? And finally when will it be executed?
In fact , if the statement in the catch is executed in the case of catching an exception, when an exception is thrown in the try statement block, the exception strength object will match the type Exception1, Exception2, Exception3 in each catch statement block in turn .....。 Once matched, the corresponding Catch statement block is entered and executed. In this case, the exception is snapped to the catch statement block
and finally statement blocks are always executed, whether or not an exception program is generated. If you execute a return statement in a try or a catch statement block, run the finally statement block first
As follows:
public class Exceptiontest {public static void main (string[] args) {try {//throw (throw) exception System.out.println ("Executes a try statement block, throws an exception "); throw new Error ();} catch (Exception e) {System.out.println ("Execute CATCH statement block, caught Exception exception"); E.printstacktrace ();} catch (Error e) { System.out.println ("Execute CATCH statement block, caught error error"); E.printstacktrace ();} finally {System.out.println ("Execute finally statement block");}}}
Operation Result:
Executes a try statement block, throws an exception Java.lang.Errorat Exceptiontest.main (EXCEPTIONTEST.JAVA:8) executes a catch statement block, has snapped to an error error executing a finally statement block
of course, when no action is done in the try, finally will execute.
Try-catch-finally exception Handling (i)