Java-25, exception handling

Abnormal Java: Java runtime errors

Observation wrong name and line number of the most important

package com.nyist;

public class TextEx {
    public static void main(String[] args) {
        int [] arr = {1,2,3};
        System.out.println(arr[3]);
    }
}

abnormal:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at com.nyist.TextEx.main(TextEx.java:6)

 

  • Java exception handler for Java provides a mechanism for error.
  • The so-called error means some unusual events occur during the running program (eg: In addition to 0 overflow, array index out of bounds, to be read in the file does not exist)
  • A well-designed program should provide a method for processing these errors when an abnormality occurs, so that the program will not block the occurrence of abnormality or produce unpredictable results.
  • Java program execution such as abnormal events, class object can generate an exception, the exception encapsulates information abnormal events and will be submitted to the Java runtime system, a process known as throw (the throw) is abnormal.
  • When the Java runtime system receives the exception object, look for code can deal with this anomaly and the current exception object to their treatment, a process known as capture (catch) exceptions.

Example:

Package com.nyist; 

public  class TextEx {
     public  static  void main (String [] args) {
         int [] ARR = {l, 2,3 };
         the try { 
            System.out.println (ARR [ . 3 ]); 
        } the catch ( ARRE An ArrayIndexOutOfBoundsException) { 
            System.out.println ( "system error, please contact the administrator: [email protected]" ); 
        } 
        System.out.println (ARR [ . 3 ]); 
    } 
}

operation result:

System error, please contact the administrator: ldw_123456 @ 163 .com 
Exception in the Thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 
    AT com.nyist.TextEx.main (TextEx.java: 11)

 

 

 

 

 Error: System Error, unable to deal with and without treatment

Exception: error can be processed, it may be catch,

RuntimeException: runtime error

 

Guess you like

Origin www.cnblogs.com/nyist0/p/12459427.html