Object-oriented exceptions

Source: Internet
Author: User
Tags stack trace

Exception

Exception: is the abnormal condition that occurs when the program is running.

The origin of the anomaly: The problem is also a specific thing in real life, can also be described in the form of Java classes, and encapsulated into objects. In fact, Java is a description of the abnormal situation after the object.

The problem is divided into two kinds: one is a serious problem, the other is a non-serious problem.

    1. For serious, Java is described by the error class. For error It is generally not written with specific code to handle it
    2. For non-critical, Java is described by the exception class. For exception can be handled with a targeted approach

Whether error or exception have some common content, such as: abnormal information, cause and so on.

Throwable

|-----Error

------XXXX

|-----Exception

------XXXX

Handling of exceptions

Java provides a unique statement to handle

Format:

Try {    catch(Exception class   variable) {    finally  {    The statement that must be executed;}

Common method operations on caught exception objects are:

    1. String getMessage (); Gets the exception information.
    2. String toString (); Get exception Name: Exception information
    3. void Printstacktrace (); Gets the exception name, exception information, the location of the exception, in fact, the JVM default exception handling mechanism, is called printstacktrace (), print the exception on the stack trace information

The sample code is as follows:

classDemo {intDivintAintb) {returnA/b;//new ArithmeticException ();    }} Public classExceptiondemo { Public Static voidMain (string[] args) {Demo d=NewDemo (); Try {            intx = D.Div (4, 0);//new ArithmeticException ();System.out.println ("x=" +x); } Catch(Exception e) {//Exception e = new ArithmeticException ();SYSTEM.OUT.PRINTLN ("except 0"); System.out.println (E.getmessage ()); ///By ZeroSystem.out.println (e);//Exception Name: Exception informationE.printstacktrace ();//Exception name, exception information, location where the exception occurred//in fact, the JVM default exception handling mechanism is to call Printstacktrace (), print the exception on the stack of trace information} System.out.println ("Over"); }}

Declare the exception (throws) on the function. Facilitates security, allows callers to process, and does not handle compilation failures.

As follows:

classDemo {intDivintAintbthrowsException {//The feature may be problematic by declaring the function with the throws keyword        returnA/b;//new ArithmeticException ();    }} Public classExceptiondemo { Public Static voidMain (string[] args) {Demo d=NewDemo (); Try {            intx = D.Div (4, 0);//new ArithmeticException ();System.out.println ("x=" +x); } Catch(Exception e) {//e.printstacktrace ();System.out.println (e.tostring ()); } System.out.println ("Over"); }}

Handling of multiple exceptions

    1. When declaring an exception, it is recommended to declare a more specific exception so that the processing can be more specific
    2. The other side declares several exceptions, which correspond to several catch blocks, and do not define redundant catch blocks. If an inheritance relationship occurs in multiple catch blocks, the parent exception catch block is placed in the last

It is recommended that you define the specific handling in catch when you are doing catch processing. Don't simply define a sentence: E.printstacktrace (), or simply write an output statement.

Examples are as follows:

classDemo {intDivintAintbthrowsArithmeticException, ArrayIndexOutOfBoundsException {//The feature may be problematic by declaring the function with the throws keyword                int[] arr =New int[A]; System.out.println (arr[4]); returnA/b;//new ArithmeticException ();    }} Public classExceptiondemo { Public Static voidMain (string[] args) {Demo d=NewDemo (); Try {            intx = D.Div (5, 0);//new ArithmeticException ();System.out.println ("x=" +x); } Catch(ArithmeticException e) {//e.printstacktrace ();System.out.println (e.tostring ()); System.out.println ("Except zero."); } Catch(arrayindexoutofboundsexception e) {//e.printstacktrace ();System.out.println (e.tostring ()); System.out.println ("The horn is out of bounds."); } Catch(Exception e) {System.out.println ("Haha:" +e.tostring ()); } System.out.println ("Over"); }}

Object-oriented exceptions

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.