Java Custom Exceptions (Exception, throws, Try-catch)

Source: Internet
Author: User
Tags throw exception

I. ...

Exception handling is a fault-tolerant processing mechanism. By constructing a trap to capture the runtime's predictable error, the error is properly handled so that the program can continue running without crashing.

Ii. who would ...

Exceptions are raised by the system environment, which is captured by the developer and left to the developer or user for processing.

Third, when do ...

Programming is not intended to handle the error immediately;

Iv. Where do ...

Anticipate impending errors, for example: preparing to write files to SDcard, but possibly SDcard not installed, which will result in file access errors.

V. How to ...

1. Defining exceptions

The exception class should be defined in its own class code file.

Inherit from standard exception (Exception),

 Public class extends Exception {    //  no parameter constructor public      customexception () {    }     // constructor    with parameter  Public customexception (String message) {        //  Call the construction method of the base class        Super( message);}    }

You can also implement Throwable to construct a custom exception.

2. Throw an exception

Exception thrown in method signature (throws):

void MyMethod () throws customexception;

Throw exception (throw) in method body:

Throw customexception;

3, catch the exception, and do the appropriate treatment

Try-catch-finally

Catch exception in try code block

Handling exceptions in Catch code blocks

Finally code block to do the cleanup work

Java Custom Exceptions (Exception, throws, Try-catch)

Related Article

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.