Java Exception Handling

Source: Internet
Author: User

An exception:

Refers to the process of running a program, something unexpected happens, such as 10 divided by 0, the file does not exist, etc.

For example: public class AMAs {

                             public void tes t333 () {
               ,         SYSTEM.OUT.PRINTLN ("Pre-run"); br>                             int a=10/0;
                        SYSTEM.OUT.PRINTLN ("post-operation");

                    }
                      public static void main (string[] args) throws exception{
    &NBSP ;                         amas   c=new amas ();
                                  c.test333 ();

}

}

System of two anomalies

There are two direct subclasses of the Throwable class: The exception class and the error class.

Exceptions are divided into run-time exceptions and compile-time exceptions.

Definition and use of try

If any of the statements in the try block have an exception, the following code will not be executed and the program will jump to the exception-handling code block, which is the catch block. Therefore, do not arbitrarily put irrelevant code in the try block, because execution can be interrupted at any time.

Definition and use of catch

A catch statement must be followed by a try statement, called a catch exception, which is the exception handler, which can be followed by a try to write multiple catch, capturing different types of exceptions, to write from subclasses to the parent class, or compile errors

The definition and use of finally

Finally the content is always executed, and there can be only one finally statement

Throw

The keyword throw is used to explicitly throw an exception after a throw exception is handled using Try-catch to catch exceptions using the throws declaration exception syntax throw new Exception class construction method; For example: throw new Exception ();

Throws

Used at the method declaration to indicate the exception thrown by the method. You can declare a variety of exception types, separated by commas.

Layer throws exception

Catch statement, after the exception is handled, the exception object is thrown again with a throw to continue to throw the exception: The method that invokes the method, the exception can be caught and handled again.

For example:

public class Sdjava {
public static void KK (Boolean isthrow) throws exception{
System.out.println ("Enter KK-----");
try{
if (Isthrow) {
throw new Exception ();
}
}catch (Exception e) {
System.out.println ("Out of the exception continued to throw");

Throw e;
}
System.out.println ("normal operation");

}
public static void Main (string[] args) {
try{
KK (TRUE);
}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("error");
}

}

}

Throw is used in the method body to throw an actual exception object, with throw, either using a try catch to catch the exception, or using throws to declare the exception.

Throws is used at the method declaration to declare the type of exception that the method may occur, and can be multiple exception types that are used to force the method to be called when the exception is handled.

Abstract methods can also use throws, so there is no throw to throws.

If you throw an exception using the Throw keyword, be sure not to use exception and not mark the exception type well.

Java Exception Handling

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.