"Javase" Java exception handling mechanism

Source: Internet
Author: User
Tags terminates throw exception

************************************************************************* * * * Original:blog.csdn.net/clark_xu Xu Changliang's Column************************************************************************1.1 ExceptionClass

There are throwable classes defined in the Java exception structure, andException and Error are the two subclasses of their derivation. Where Exception represents an exception due to an illegal situation , error indicates a bug in the Java Runtime environment.

The purpose of exception handling is to properly terminate the program when an exception occurs, to avoid catastrophic consequences, and the specific operations usually include:

l Notification: Notifies the user of the occurrence of an exception pair

l Restore: Save important data, close files, rollback transactions, etc.

l exit: Exit the program in the proper form

1.2 TryStatement

The try statement specifies a piece of code that changes the scope of the capture and processing, and the catch statement after it corresponds to any exceptions that the code might throw.

1.3 CatchStatement

Each try statement block can be accompanied by multiple catch statements to handle the different types of exceptions that may occur. The exception declared in the catch (Cath (someexception e)) encapsulates information about the occurrence of an exception event, which can be used in a catch statement block to obtain this information, including The GetMessage() method gets information about the event of the exception, and theprintstacktrace() method is used to track the contents of the execution stack when an exception event occurs.

1.4 ThrowStatement

In the first scenario, this exception is thrown when the program has an error but cannot handle it.

The second scenario, the active self-throwing exception:thrown new ArithmeticException ();

The third scenario, when the exception is handled since, is thrown, allowing the next layer of exception handling blocks to be captured.

An example of an active throw exception.

try{

if (dblzero==0) {

Thrown new ArithmeticException ();

}

}

Cath (Arithmeticexepton) {

exception occurred except 0

}

1.5 throwsStatement

There are many methods in the object that may throw an exception due to some errors, but do not want to handle these exceptions directly in this method, and want to call this his method for uniform processing. You can use the throws keyword to declare the exception thrown by this method. The format is :

Pubic static void Stringtodate (String str) thows parseexception{

//

}

The call in the main method is:

try{

Stirngtodate (str);

}

catch (ParseException e) {

E.printstacktrace ():

}

1.6 FinallyBlock

The finally statement provides a unified exit for exception handling, which enables unified management of the state of the program, along with the other parts of the program. In other words, the code specified by thefinally lock is executed regardless of whether an exception is thrown in the program specified by the try statement

Therefore, it is usually possible to eliminate information resources in the finally statement, such as closing open files,jdbc connections, deleting temporary files, and so on.

1.7 try-catch-finallyUse

You must add catch or finally blocks after the try block; You can nest try-catch-finally structures.

You can thown throw exceptions in try-cath-finnally and wait for other catch statements to be processed.

Note that the following procedure terminates abnormally at finally

l JVM terminates prematurely

An unhandled exception is thrown in the L finally block

1.8 RuntimeException

Java Exceptions can also be categorized as: Detectable exceptions ( compiler-enforced exceptions ), non-detection exceptions (such as runtimeexception), custom exceptions.

The common runtimeexception have

l illegalargumentexception

l nullpointerexception

l arrayindexoutofboundsexception

l classcastexception

l numberformatexception

In multiple catch handles a series of exceptions, the size of the exception, and the large type are placed behind the processing.

1.9Exception handling When overriding a method

If you use inheritance, throws some exceptions on a method in the parent class, and redefine the method in a subcategory, you can:

L override method can not handle exception (when redefined, do not set throwns)

L override method can throws the exception subclass of the parent method

L override method can throws some exceptions of the parent method

However, you cannot:

l re-method throws The parent method has no throws exception

"Javase" Java exception handling mechanism

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.