First, the basic concept
(1) Exception : An abnormal condition that occurs during runtime of a Java program.
Java describes and encapsulates an object's behavior in terms of object-oriented thinking.
(2) Exception classification : (throwable: Defines the functionality that is common to the problem. )
1.Error: From the bottom of the system, JVN tells the user. Do not do targeted processing, directly modify the code.
2.Exception: Occurs and tells the user, can be targeted to deal with.
(3) Abnormal handling situation :
1. The problem is not dealt with in detail and continues to be thrown to the caller. is to declare the exception through the throws keyword on the function. The goal is for callers to be able to handle it.
2. Targeted Treatment: Capture!
Try
{
There is a possibility that the exception code will occur.
}
catch (Exception class variable)
{
This is the real catch, the code that handles the exception;
}
Finally
{
Code that is bound to be executed.
}
Second, exception handling code (i)
1 classDemo2 {3 intDivintAintbthrowsException//declaring exception Exception4 {5 returnA/b;6 }7 }8 9 classExceptionDemo1Ten { One Public Static voidMain (string[] arge)throwsException//throwing exception exception to virtual machine processing A { -Demo d =NewDemo (); - intnum = D.Div (4,0); theSystem.out.println ("num=" +num); -System.out.println ("Over"); - } -}
Third, exception handling code (II)
1 classDemo2 {3 intDivintAintbthrowsException//declaring exception Exception4 { 5 returnA/b;//throw new ArithmeticException ("/By Zero") is thrown to the caller. 6 }7 }8 9 classExceptionDemo1Ten { One Public Static voidMain (string[] arge) A { -Demo d =NewDemo (); - the //to handle an exception - Try - { - intnum = D.Div (4,0); +System.out.println ("num=" +num); - } + Catch(Exception e) A { at //you can work with this object by using the object's methods. -System.out.println ("code to handle exception: divisor cannot be 0"); -System.out.println (E.getmessage ());//Exception Information -System.out.println (E.tostring ());//Exception name + exception information -E.printstacktrace ();//Exception name + exception information + location. The JVM's default handling of receiving exceptions is to call this method. Displays the information on the screen. - } inSystem.out.println ("Over"); - } to}
Java Object-oriented exception (exception handling mode)