First of all, what is the exception:
Exception is the program abnormal end, not according to our processing logic to carry out the situation caused by the program interruption.
Generally speaking, the first error in writing a program is basically null pointer nullpointerexception array out-of-bounds parameter exception
In fact, Java added exception handling mechanism to isolate exceptions and normal code, to ensure the robustness and integrity of the program
First look at the segment code
public static void Main (string[] args) {try {arraylist<integer> nin = new arraylist<integer> (); Nin.add (312); Nin.add (3122); Nin.add (3112); System.out.println (Nin.get (8)); } catch (Exception e) {System.out.println ("Exception"); E.printstacktrace (); } catch (ArrayIndexOutOfBoundsException e) {System.out.println ("arrayindexoutofboundsexception"); E.printstacktrace (); }}
Hey.... I go to the code will not compile the past.
public static void Main (string[] args) throws NullPointerException {Show ();} public static int[] Nu = null;public static void Show () {System.out.println (dosomething ()); try {System.out.println (nu[3] );} catch (NullPointerException e) {System.out.println ("nullpointerexception-"); E.printstacktrace ();} catch (Exception e) {System.out.println ("Exception-"); E.printstacktrace ();}}
Output Result:
NullPointerException-
Java.lang.NullPointerException
at Com.tz.TestException.show (testexception.java:26)
at Com.tz.TestException.main (testexception.java:16)
The original Java catch is in order to match down, and once catch to the exception to meet the requirements, you will jump directly, there is a finally will execute it, not directly jump out, no longer execute anything, so catch exception should have a sequence.
The catch code block should be written down from a small range to a wide range of exception sequences.
public static int[] Nu = null;public static void Show () throws NullPointerException {try {System.out.println (n U[3]); }catch (Exception e) {System.out.println (""); E.printstacktrace (); }}
The following code
Exception-
Java.lang.NullPointerException
at Com.tz.TestException.show (testexception.java:26)
at Com.tz.TestException.main (testexception.java:16)
public static int[] Nu = null;public static void Show () {System.out.println (nu[3]); try {}catch (Exception e) {System.out.println ("Exception-"); E.printstacktrace (); }}
The following code
Exception in thread "main" Java.lang.NullPointerException
at Com.tz.TestException.show (testexception.java:23)
at Com.tz.TestException.main (testexception.java:16)
so Try......catch is the exception to the code it contains, method throws exception in the method;
The exception inside the try catch is thrown by it, the exception of the code appearing in the Catcha is thrown by the throws exception on the method; multiple Catch exceptions, if there is a parent-child relationship, is the first-write child exception above, the parent exception is below, This compilation will not go wrong;
The Java exception structure diagram is as follows:
650) this.width=650; "src=" http://img.my.csdn.net/uploads/201211/27/1354020417_5176.jpg "alt=" 1354020417_5176.jpg "/>
From the above can be seen throwable two kinds of one is an anomaly is an error (in this case the program basically kneeling to recover)
Java exceptions are divided into two types of checked exceptions (compile-time exceptions) and runtime exceptions (runtime Exceptions)
Exception Handling rules:
Try catch exception--catch handling exception or custom exception logic handling code--What to do when the finally ends
How to throw an exception?
1. Try....catch
2. Throws exception on method
3. Throw new xxxexception inside the code block
4. the code block inside the throw new xxxexception framework uniformly handles the captured exception information
Handling Throw Mode:
(1) If you know exactly what to do with the exception or what might happen, you should throw the exception in Try....catch and write down the exception code.
(2) If you do not know what is abnormal, then throw an exception on the method, multiple exceptions comma separated.
Note: Java runs in pairs (runtimeexception) exception even if you do not capture the thrown handle it will also help you to throw out the fact that ..... Now most of the Java7 start projects are throwing exceptions to the top, which is the control layer C re-processing.
This article is from the "JTR" blog, make sure to keep this source http://10229180.blog.51cto.com/10219180/1886167
Java exception Processing