Java Object-oriented exception (exception handling mode)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.