Java exception (Novice beginner)

Source: Internet
Author: User

Exception handling mechanism exists mainly for the program in the run, if there is an exception, the program can be uninterrupted, and continue to run.

Exceptions are classified as run-time exceptions, and non-runtime exceptions.

If it is a run-time exception, we usually do not handle it.

Rather than exceptions, we handle them in several ways.

-----------------------------------

If you are dealing with exceptions in this method, we usually use a try statement block to handle

The content in the Try statement is the lookup exception, and if an exception is found, the subsequent catch statement

Capture, and make the processing you want, a try statement can have a lot of catch statements

However, because exceptions are mostly inherited from the parent class, catch multiple exceptions when

The exception class must be large to small, because if the parent class is in front, an exception occurs

Will be captured by the first parent class, and subsequent catch will not allow

This is a little similar to the parent class reference of the subclass object.

Finally statement

This statement must be executed, whether or not an exception occurs.

Add:

If there is a return statement in the try Block

His role must be to execute the contents of the Finally block first

will be implemented, and the statement after finally will not be implemented

------------------------------------

Usually, we encounter anomalies like this.

Exceptions may occur in a method

Call a method in the B method

At this point, we generally throw exceptions in the A method.

Then capture in the B method (that is, use a try block)

A method throws an exception by throws the exception class after the method name (equivalent to declaring it)

Then throw the new exception class in the method.

So the A method throws the exception and then the B method can catch

And if we continue behind the main method throws

This time the exception continues, while the virtual machine is processed.

Java exception (Novice beginner)

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.