Java provides two primary exceptions: Runtime Exception and Checked Exception. All Checked Exception is derived from the Java.lang.Exception class, and Runtime Exception is derived from java.lang.RuntimeException or java.lang.Error class.
Their differences are expressed in two aspects: mechanisms and logically. First, the mechanisms are different in mechanisms in the mechanism at two points: 1. How to define methods; 2. How to deal with the abnormal exceptions. Please see the definition of the CheckedException below:
Public class checkedException Extends Exception {public checkException () {} public checkedException (String message) {super (message);}}
And an example using Exception:
public class ExceptionalClass {public void method1 () throws CheckedException {// ... throw new CheckedException ( "... is wrong");} public void method2 (String arg) {if (arg == null) {throw new NullPointerException ("Parameters of Method2 are NULL!");}} public void method3 () throws checkException {method1 ();}}
You may have noticed that both methods Method1 () and Method2 () will throw Exception, but only Method1 () has declared. In addition, Method3 () itself does not throw an Exception, but it declares will throw CheckedException. Let's take a look at this type of main () method before explaining you.
public static void main (String [] args) {ExceptionalClass example = new ExceptionalClass (); try {example.method1 (); example.method3 ();} catch (CheckedException ex) {} example.method2 (null);}
In the main () method, if you want to call Method1 (), you must put this call in the TRY / CATCH program block because it will throw Checked Exception. In contrast, when you call Method2 (), you don't need to put it in the Try / Catch block, because it will thrown exception, but Runtime Exception. Will throw Runtime Exception When defining it, it is not necessary to declare it will throw an Exception. Now let's take a look at Method3 (). It calls Method1 () but did not place this call in the TRY / CATCH block. It is to avoid this by declaring it will throw Method1 () Exception. It didn't capture this Exception, but passed it. In fact, the main () method can also do this, by declaring it will throw Checked Exception to avoid using the TRY / CATCH block (of course we oppose this approach). Summary: * Runtime Exceptions: No declaration when defining methods will throw Runtime Exception; do not need to capture this runtime exception when calling this method; Runtime Exception is derived from java.lang.RuntimeException or java.lang.Error class of. * Checked Exceptions: You must declare the check exception that may be thrown when you call this method, you must capture its checked exception, otherwise you have to pass its Exception; Checked Exception is from java.lang.exception Class derived. Second, logically from logic's perspective, Checked Exceptions and Runtime Exception have different purposes. Checked Exception is used to indicate an abnormality that a caller can directly process. Runtime Exception is used to indicate a program error that a caller itself cannot handle or recover. Checked Exception forces you to capture it and handle this abnormal situation. Take the constructor of Java.Net.ur to take a MalforMedurLexception. MalformedurLexception is a checked exception. Imagine, you have a simple program to prompt users to enter a URL and then download a web page through this URL. If the URL entered by the user has an error, the builder will throw an Exception. Since this exception is Checked Exception, your program can capture it and correctly handle: For example, the user re-entered. Look at the following example: public void method () {int [] Numbers = {1, 2, 3}; int sum = numbers [0] NumBers [3];