The basic concept of abnormal
The program is running, there is a program operation can not be performed while the cause of the interruption.
An exception is an event occur during the execution of a program, he interrupted the normal operation command,
Blocker is a normal operation of the error
It is a special object, Exception subclass
Java errors
Serious problem a reasonable application does not intercepted, such as a failure of VM
Error are also objects is a subclass of Error
Abnormal program itself can be processed by the program and the external environment is caused by
Originally built-in error system is generally unable to handle the programmer does not need to deal with
Runtime exception
RuntimeException class and its subclasses are exceptions that are typically exempt abnormalities, such as ArithmeticException, NullPointException, IndexOutOfBoundsException
Abnormal non-runtime
Exception belong to the type class and its subclasses, need to be processed. Such as: abnormal IOException, SQLException and user-defined
Exception Handling
step:
1, throw an exception: when semantic constraints are violated will throw (the throw) abnormalities, i.e. an exception event, an exception object generated, and submit it to a running system, and then look for the corresponding code from the operating system to handle exceptions
2, catch the exception: the exception is thrown, the runtime system begins to generate exception object code, the method call stack eye start looking until you find appropriate treatment method code indulgence, and the exception object to the method so far
java.util.Scanner Import; public class StringDemo { public static void main (String [] args) { Scanner INPUT = new new Scanner (the System. in ); the System. OUT .println ( " Please enter the dividend: " ); int X = input.nextInt (); the System. OUT .println ( " Please enter the divisor: " ); int Y = input.nextInt (); // Y = 0 exception occurs the System. OUT .println (Divide (X, Y)) ; } public static int divide(int x,int y) { return x/y; } } //y=0 时,抛出异常 /* Exception in thread "main" java.lang.ArithmeticException: / by zero at StringDemo.divide(StringDemo.java:14) at StringDemo.main(StringDemo.java:11) */
Abnormal, the program is still running arithmetic exception operation belongs
The inevitable exceptions, such as a network outage, memory overflow, how to deal with?
Disposition:
1, using the try ... catch ... finally statement to handle exceptions
The first step in using try {....} catch exceptions sentence block selected range, that is, the exception may trigger code included in the try block
A try block may be accompanied by one or more catch statement, a plurality of catch blocks, treated by a different exception
finally block: whether or not an exception occurs, will be executed
Import java.util.InputMismatchException; Import java.util.Scanner; public class StringDemo { public static void main (String [] args) { the try { Scanner INPUT = new new Scanner (the System.in); System.out.println ( "Please enter the dividend: " ); int the X-= input.nextInt (); System.out.println ( " Please enter the divisor: " ); int the y-input.nextInt = (); // the y-appear abnormal = 0 int RES = 0 ; RES =Divide (X, Y); System.out.println (RES); } the catch (an ArithmeticException E) { System.out.println ( "Exception divisor" ); } the catch (a InputMismatchException E) { System.out.println ( "Enter exception " ); } the finally {
System.out.println (" whether abnormality will be executed ")
} } public static int Divide ( int X, int Y) { return X / Y; } }
2, using the keyword throws an exception transfer
Import a java.io.FileInputStream; Import java.io.FileNotFoundException; public class StringDemo { public static void main (String [] args) throws a FileNotFoundException { testThrows (); } public static void testThrows () throws a FileNotFoundException { the FileInputStream FIS = new new the FileInputStream ( "test.txt" ); } } // throws an exception, throw up, who caused an exception, who will be responsible