Abnormal system:
----------| Throwable the superclass of an exception or error class
--------------| Error error errors are generally used for JVM or hardware-initiated problems, so we do not normally use code to handle errors.
--------------| Exception exceptions need to be handled by code.
How to differentiate between errors and exceptions:
If the program has an unhealthy message, if the class name of the unhealthy information ends with an error, then it must be a mistake.
If it ends in exception, it's definitely an exception.
Handling of Exceptions:
Mode one: Capture processing (there is also a way below two )
Format of capture processing:
try{
Code that could have an exception occurred;
}catch (catch exception type variable name) {
Code to handle the exception ....
}
Capture the details of the processing to be noted:
1 .code1 → If the code in the try block has been processed, the code outside the Try-catch block will execute normally.
2.code2 → If the code in the try block has an exception, the code that follows the exception code in the try block will not execute.
3.code3 in a try block can be followed by multiple catch blocks, that is, a try block can catch a variety of types of exceptions.
4.code4 → a try block can catch a variety of types of exceptions, but the type of exception caught must be captured from small to large, or compile an error.
Code1
Package Day10;public class TestException2 {public static void main (string[] args) {test (1,0);} public static void Test (int a, int b) {Try{int c = A/b;} catch (ArithmeticException e) {System.out.println ("exception handles and returns the full class name of the current exception object + morbid Information:" +e.tostring ());} System.out.println ("The code outside the Try-catch block can be performed normally");}}
Operation Result:
The exception handles the full class name of the current exception object + morbid Information: java.lang.ArithmeticException:/by zero
Code outside the Try-catch block can be executed normally
Code2
Package Day10;public class TestException2 {public static void main (string[] args) {test (1,0);} public static void Test (int a, int b) {Try{int c = A/b; SYSTEM.OUT.PRINTLN ("See if it is executed to ~");} catch (ArithmeticException e) {System.out.println ("Exception processed and returns the full class name of the current exception object + morbid Information:" +e.tostring ());}}}
Operation Result:
The exception handles and returns the full class name of the current exception object + morbid Information: java.lang.ArithmeticException:/by zero
"See if it is executed to ~" is not executed, because int c = A/b, this is an exception, so the rest of the code inside the try block will not execute and will be handled directly by the catch.
Code3
public class TestException2 {public static void main (string[] args) {int[] arr = null;test (10,2,arr);} public static void Test (int a, int b, int[] arr) {int c = 0; try{c = A/b; System.out.println (arr.length);} catch (ArithmeticException e) {System.out.println ("exception handled and returns the full class name of the current exception object + morbid Information:" +e.tostring ());} catch (NullPointerException e) {System.out.println ("a null pointer exception has occurred ...."); catch (Exception e) {System.out.println ("CC");} System.out.println ("c=" +c);}
Operation Result:
A null pointer exception has occurred ....
C=5
Code3 This code should note that the try block can detect a variety of exceptions, but only one exception is the first exception.
Code4
public class TestException2 {public static void main (string[] args) {int[] arr = null;test (10,2,arr);} public static void Test (int a, int b, int[] arr) {int c = 0; try{c = A/b; System.out.println (arr.length);} catch (Exception e) {System.out.println ("Exception class to capture, other classes can not capture, the equivalent of the parent class to do the work done, and the subclass of things?" ");} catch (NullPointerException e) {System.out.println ("a null pointer exception has occurred ...."); catch (ArithmeticException e) {System.out.println ("CC");} System.out.println ("c=" +c);}
Operation Result:
Exception the exception of NullPointerException and ArithmeticException is handled, so it will prompt the error, must be captured from small to large, or compile an error
Mode two: Exception handling method----Throw processing
throw the details of the processing to note:
1.embodied in the CODE5→If an exception object is thrown inside a method, it must be declared on the method.
2.embodied in the Code6→If a method that declares an exception is called, the caller must handle the exception.
3.embodied in the Code7→If an exception object is thrown inside a method, the code after the throw statement is no longer executed (a method encounters the Throw keyword and the method stops executing immediately).
Embodied in the 4.code8→In one case, only one type of exception object can be thrown. (meaning if ())
throw and throws two keywords:
1. The throw keyword is used inside the method, and throws is used for method sound declarations.
2. The throw keyword is used to throw an exception object inside a method, and the throws keyword is used to declare the throw exception type on the method declaration.
3. There can be only one exception object after the Throw keyword, and throws can declare multiple types of exceptions at a time after
Code5
public class TestException2 {public static void main (string[] args) {try{int[] arr = null;test (10,2,arr);} catch (NullPointerException e) {System.out.println ("Report! "); E.printstacktrace ();}} public static void Test (int a, int b, int[] arr) throws Arithmeticexception,nullpointerexception{int c = 0; if (b==0) {thro W new ArithmeticException (); Throws an exception object ...} else if (arr==null) {throw new NullPointerException ();} c = A/b; System.out.println (arr.length); System.out.println ("c=" +c);}
Operation Result:
Report! There's an anomaly.
Java.lang.NullPointerException
At Day10. Testexception2.test (testexception2.java:22)
At Day10. Testexception2.main (TESTEXCEPTION2.JAVA:10)
Code6
public class TestException2 {public static void main (string[] args) {//try{int[] arr = null;test (4,0,arr);//Call a declaration to throw an exception class Type of method//}catch (Exception e) {System.out.println ("Report! Exception ");//e.printstacktrace ();//}}public static void Test (int a, int b, int[] arr) throws Exception,nullpointerexception {int c = 0; if (b==0) {throw new Exception ();//Throw an exception object ...} else if (arr==null) {throw new NullPointerException ();} c = A/b; System.out.println (arr.length); System.out.println ("c=" +c);}
Operation Result:
P.S.
The code in this code6, it is important to note that in the test method thrown if the RuntimeException class, is not captured and will not prompt for error, because the exception is divided into the inspection of abnormal and run-time exception check exception needs to catch, run-time exception does not force the demand for capture, Which means that non-runtimeexception including eception are needed to capture
Code7
public class TestException2 {public static void main (string[] args) {try{int[] arr = null;test (4,0,arr);//Call a declaration to throw an exception type Method}catch (Exception e) {System.out.println ("Report! The exception appears "); E.printstacktrace ();}} public static void Test (int a, int b, int[] arr) throws Exception,nullpointerexception{int c = 0; if (b==0) {throw new Exce Ption (); Throws an exception object ...} else if (arr==null) {throw new NullPointerException ();} c = A/b; System.out.println (arr.length); System.out.println ("c=" +c);}
Operation Result:
Report! There was an anomaly.
Java.lang.Exception
At Day10. Testexception2.test (testexception2.java:21)
At Day10. Testexception2.main (testexception2.java:11)
It is visible that the value of C is not printed, so the code behind the throw exception is not executed.
Code8
Class Demo11 {public static void main (string[] args) {try{int[] arr = Null;div (10,0,arr);//Call a method declaring the exception type to be thrown}catch (excep tion e) {System.out.println ("Report! Abnormal "); E.printstacktrace ();}} public static void Div (int a, int b,int[] arr) throws Exception,nullpointerexception {if (b==0 | | arr==null) {throw new Exce Ption (); Throws an exception object ... throw new NullPointerException (); int c = A/b; System.out.println ("c=" +c);}
Operation Result:
Because the exception is thrown, the following statement is not processed, the method ends, so only 2 exceptions can be separated into two cases to avoid compile-time errors.
if (b==0) {throw new Exception ();//Throws an exception object ...} else if (arr==null) {throw new NullPointerException ();}
AC Penguin: 654249738, and self-taught Exchange group: 517284938
Java_se base--54. Exceptions