The simple principle and application of exception handling mechanism in Java, and explain what is the difference between error and exception?

Source: Internet
Author: User
Tags exception handling throwable

When Java programs violate Java semantic rules, the Java Virtual machine will represent errors that occur as an exception

The violation of the semantic rules consists of two cases:

One is a semantic check built into the Java class library, such as an array of bounds that throws indexoutofboundsexception; a null-accessing object throws a NullPointerException

Another scenario is that Java allows programmers to extend this syntax check, programmers can customize exceptions, and freely choose when to throw an exception with the Throw keyword, all of which are java.lang.Throwable subclasses

Error and Exception:

The error and exception in Java are all from Throwable this class, is his subclass

Error: Is the system of errors, programmers can not change the processing of this is in the process of compiling the error, only by modifying the program to correct the error

Exception: The error that is caught when the program is running, he is the exception that can be handled

Exception: It is also divided into two types: runtimeexception (which can often occur without catch) and other Exception (which must be catch, so that the program can continue, like a method with throw IOException).

One

* Runtime Exceptions:
When you define a method, you do not need to declare that it throws runtime exception; You do not need to capture this runtime exception when you call this method; runtime Exception are derived from Java.lang.RuntimeException or Java.lang.Error classes.
* Checked Exceptions:
When you define a method, you must declare all checked exception that might be thrown, and when you call this method, you must capture its checked exception, or you will have to pass it exception; checked Exception is derived from the Java.lang.Exception class.
Second, logically
From a logical point of view, checked exceptions and runtime exception are used for different purposes. Checked exception is used to indicate an exception that a caller can handle directly. Runtime exception is used to indicate a program error that the caller itself cannot handle or recover.

Checked exception forces you to capture it and handle this anomaly. As an example of a Java.net.URL class builder (constructor), each of its builders throws malformedurlexception. Malformedurlexception is a kind of checked exception. Imagine that you have a simple program that prompts the user to enter a URL and then downloads a Web page through the URL. If the user enters a URL with an error, the builder throws a exception. Since this exception is checked exception, your program can capture it and handle it correctly: For example, prompt the user to re-enter.

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.