Java exception mechanism

Source: Internet
Author: User

The Java exception mechanism relies primarily on try,catch,finally,throw,throws five keywords, where the try keyword is followed by a curly brace (not omitted, even if there is only one line of code), where the code that might throw the exception is placed. A catch corresponds to an exception type and a code block that indicates that the catch block is used to handle this type of code block. Multiple catch blocks can also be followed by a finally block that recycles the physical resources opened in the try block, which guarantees that the finally block is always executed, and that the throws block is used in the method signature to declare the possible exceptions that the method throws. The throw can be used as a single statement to throw a specific exception.

Java exceptions are divided into checked exceptions and runtime exceptions, checked exceptions are exceptions that can be handled at compile-time, and all Java enforces all checked exceptions
Runtime exceptions do not need to be handled.

Java divides non-normal situations into two types, exceptions (Exception) and errors (error), and they all inherit the Throwable parent class

Error errors generally refer to virtual machine-related issues such as system crashes, virtual machine errors, dynamic link failures, and so on, which can be unrecoverable or impossible to capture, causing the program to break. Typically, applications cannot handle these errors.

Exception abnormal ...

When an exception is caught, all the catch blocks of the parent exception are queued after the subclass exception catch block (that is, the small exception is processed first, and then the large exception is processed) or a compilation error occurs.

Typically, when a generic method executes to a return or throw statement, the program ends the method immediately, but if there is a finally block, it must first execute the code in the finally block before executing the return or Throw statement end method. The finally block is bound to be executed unless you use the System.exit (1) statement to eject the virtual machine.

In general, do not use statements such as return or throw that cause methods to terminate in a finally block, which would otherwise invalidate the try block, the return in the catch block, and the throw statement.

When using the throws declaration to throw an exception, there is a restriction that the exception type declared in the subclass method should be a subclass of the parent class method declaration that throws the exception type or equal, and the subclass method does not allow more exceptions than the parent class method declaration to throw.

Examples of custom exception class templates:
public class Auctionexception extends Exception
{
Public auctionexception () {}
Public auctionexception (String msg)
{
Super (MSG);
}
}

There are two ways to handle exception handling
1 The exception is caught and handled within the method where the exception occurred, and the caller of the method will not be able to catch the exception again
2 The method signature declares an exception to be thrown, and the exception is completely given to the method caller for processing

Example: Working with multiple methods to collaborate on an exception case
public class Testauction {

/**
* @param args
*/
Private double initprice = 30.0;
public void Bid (String bidprice) throws auctionexception{
Double d = 0.0;
try{
D = double.parsedouble (Bidprice);
}
catch (Exception e) {
E.printstacktrace ();
throw new Auctionexception ("input error");
}
if (initprice>d) {
throw new Auctionexception ("The value is too small");
}
Initprice = D;
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
Testauction ta = new Testauction ();
try{
Ta.bid ("10");
}
catch (Auctionexception e) {
System.out.println (E.getmessage ());
}
}

}

It is irresponsible to pass the underlying primitive exception directly to the user, usually by capturing the original exception, then throwing a new business exception, and the new business exception contains the user's prompt information, which is referred to as abnormal interpreting (one of the application of the chain of responsibility).

Exception handling target:
1 Minimizing program code clutter
2 Capturing and preserving diagnostic information
3 informing the right person
4 ending unusual activity in the right way

Basic rules for exception handling:
1 Do not overuse exceptions (exception handling is intended to separate unpredictable exception handling code from normal business code, so never use exception handling as a substitute for normal business logic)
2 Do not use an overly large try block
3 Avoid using catch all statements
4 do not ignore the caught exception

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.