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.