A preliminary study of exception handling in Java

Source: Internet
Author: User

There are several exceptions in Java:

1) Error:java internal Error during operation;

2) Exception: Exceptions that should be caught in the program.

RuntimeException: errors caused by programming;

IOException: Error generated by input and output.

Method of exception handling:

1) Try-catch


A try statement block can have only one, and a catch can have multiple, and a catch must follow the try statement with no other code in between. If there are multiple catch statements, the catch statement that captures the parent exception must be placed behind it, otherwise it will catch all of its subclass exceptions, leaving the subclass exception catch statement to never execute.

2) finally

When an exception occurs, the program jumps out of the currently running statement block, finds the exception capture statement block, and then jumps back to the program to execute the catch statement.

However, sometimes after an exception, some statements must be executed, this time can be used finally. It specifies that the block of statements be executed anyway. There can be only one finally statement block in a try-catch. Also, in general, the finally statement block is placed after the last catch statement block.

3) throws

Throws an exception in the method, the handling of the exception is left to the caller of the method, an exception is thrown in the method definition, and when the method is called, the method call statement must be placed in the Try-catch statement block and the corresponding exception is caught in the catch.

A preliminary study of exception handling in Java

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.