Java Exception Handling

Source: Internet
Author: User

Exception class:

In Java, it describes and encapsulates an object in the form of a class, and the exception is that Java encapsulates the problem as an object through object-oriented thinking.

exceptions occur for a number of reasons, usually including the following categories:

    • The user entered illegal data.
    • The file you want to open does not exist.
    • The connection is interrupted during network communication, or the JVM memory overflows.

Throwable:

The Exception class is a subclass of the Throwable class. In addition to the exception class, Throwable also has a subclass error.
The characteristic of this system is that throwable and all its subclasses have parabolic properties.
What does it mean to be parabolic? How can we embody the parabolic nature? In fact, through two key words to reflect.
Throws throw, all classes and objects that can be manipulated by these two keywords have a parabolic nature.
|--1, generally not handled: Error
Feature: Is the problem of severity thrown by the JVM. This problem occurs generally not targeted treatment, directly modify the program.

|--2, can be handled by: Exception

Classification of exceptions:
1, compile-time is detected exception: As long as the exception and its subclasses are, in addition to the special sub-class RuntimeException system.
Once this problem occurs, it is hoped that it will be detected at compile time, so that the problem can be handled in a corresponding way. Such problems can be dealt with in a targeted way.
2, compile-time does not detect the exception (run-time exception, more than see ): Is exception in the RuntimeException and its subclasses.
This problem does not allow the function to continue, the operation can not be carried out, more because of the caller's cause or caused by the internal state changes caused.
Then this problem is generally not handled, directly compiled through, at run time, let the caller call the program to force stop, let the caller to modify the code.

Custom Exceptions:

Some exceptions are not defined in Java, and you can customize the description of exceptions according to the idea of Java exception creation and object-oriented. and encapsulated into objects.

When customizing exceptions, either inherit exception. or inherit runtimeexception.

* Inheritance exception need throws to declare, inheritance runtimeexception not required. *

The purpose of declaring exceptions is for callers to handle these problems.

The difference between throws and throw:

1,throws used on a function, throw is used within a function.
2,throws throws an exception class that can be thrown multiple, separated by commas. Throw throws an exception object.

catch form of exception handling:

This is how the exception can be dealt with in a targeted manner.

The specific format is:

Java Exception Handling

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.