Java Error and Exception differences

Source: Internet
Author: User

Both error and exception inherit from Throwable;

The difference between the two:

Exception:

1. Can be controllable (checked) or uncontrolled (unchecked);

2. Indicates an error caused by the programmer;

3. Should be processed at the application level;

Error:

1. Always uncontrollable (unchecked);

2. Often used to indicate system errors or underlying resource errors;

3. If possible, it should be captured at the system level;

Two types of exceptions are defined in Java:
1) Checked exception: This type of exception is a subclass of exception. The exception is handled by an up-throw mechanism, and if the subclass can produce a exception, then the throws a exception must be in the parent class. Possible problems: Low code efficiency, high coupling.
  
2) Unchecked exception: This kind of anomaly is runtimeexception subclass, although RuntimeException is also a subclass of exception, but they are extraordinary, they cannot pass the client Code to try to solve, so called unchecked exception.

The Java language requires that all exceptions thrown (throw) must be derived from Throwable in a Java program (regardless of who wrote the code). Of course, in the actual Java programming, because the JDK platform has already designed the very rich and complete exception object classification model for us. As a result, Java programmers generally do not need to redefine their own exception objects. And even if you need to extend a custom exception object, it is often derived from exception. So, for Java programmers, it is generally only necessary to catch (Exception ex) in its top-level function to catch all the exception objects. The base class for all exception objects is throwable, which throwable directly from object (which is mandatory by the Java system), and it implements the Serializable interface (This is the most sufficient material preparation for all exception objects to easily cross the Java component System).

The exception classes derived directly from Throwable have exception and error. Exception is the most familiar to Java programmers, and it generally represents the fundamental class of real-world exception objects. In other words, Exception and all exceptions derived from it are the types of exceptions that the application can catch and which can be handled for exception-error recovery. Error indicates that there is a very serious exception error in the Java system, and that the error may not be recoverable by the application, such as Linkageerror, or Threaddeath.

Java Error and Exception differences

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.