What to look for in Java exceptions

Source: Internet
Author: User
Tags finally block

Exceptions are always first thrown and then captured.

It is important to note that once a catch catches a matching exception type, it enters the exception handling code. Once the processing is finished, it means that the entire Try-catch statement ends. Other catch clauses no longer have an opportunity to match and catch exception types. So for an exception program with multiple catch clauses, you should try to put the catch clause that captures the underlying exception class in front of you as much as possible, and keep the catch clause of the exception class that captures the relative high-level as far back. Otherwise, the catch clause that captures the underlying exception class may be masked.

The runtimeexception exception class includes various common exceptions at run time, and the ArithmeticException class and ArrayIndexOutOfBoundsException class are subclasses of it. Therefore, the catch clause of the RuntimeException exception class should be placed on the last side, or it might mask the subsequent specific exception handling or cause a compilation error.

In the following 4 special cases, the finally block is not executed:
1) An exception occurred in the finally statement block.
2) exit the program with System.exit () in the previous code.
3) The thread on which the program is located dies.
4) Turn off the CPU.

When a try catches an exception, there is a case in the catch statement block that handles this exception: in the TRY statement block is executed in order, when the execution of an exception to a statement, the program jumps to the catch statement block and matches the catch statement block one by one, to find the corresponding handler, The other catch statement blocks will not be executed, and the statement after the exception is not executed in the TRY statement block, after the catch statement block executes, the statement in the finally statement block is executed, and finally the statement after the final statement block is executed;

Throws throws an exception to the rule:

1) If the exception is not checked (unchecked exception), that is, error, runtimeexception, or their subclasses, you can declare the exception to be thrown without using the throws keyword, the compilation will still pass smoothly, but will be thrown by the system at runtime.

2) You must declare any checked that the method can throw (exception). That is, if a method may be subject to a Try-catch exception, either by using the throws clause declaration to throw it, or it will result in a compilation error.

3) only if an exception is thrown, the caller of the method must handle or re-throw the exception. When the caller of the method is unable to handle the exception, it should continue to be thrown, rather than swallowed.

4) The calling method must follow any processing and declaration rules that can be checked for exceptions. If you override a method, you cannot declare an exception that differs from the overriding method. Any exception declared must be the same or parent class of the exception declared by the overridden method.

What to look for in Java 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.