Java Exceptions: Choose checked Exception or unchecked Exception?
Java contains two types of exceptions:
Checked ExceptionAnd
Unchecked Exception。
The difference between checked and unchecked exceptions is that the checked exception must be explicitly captured or passed (thrown up and propagated along the call stack to propagate the exception), such as try-catch-finally, throws Exception. Unchecked exceptions can be not captured or thrown.
The checked exception inherits from the Java.lang.Exception Class (General exception). The unchecked exception inherits from the Java.lang.RuntimeException class (Run-time exception).
There is a lot of support or opposition to whether the two should even use checked anomalies. This article will discuss some common points of view. Before you begin, clarify a question:
Checked and unchecked exceptions are equivalent in terms of functionality. Functions that can be implemented with checked exceptions can be implemented with unchecked exceptions and vice versa.
Choosing checked or unchecked abnormalities is a matter of personal habits or organizational rules. There is no question of who is stronger than who. a simple example
Before discussing the pros and cons of checked and unchecked exceptions, look at the code to use them as follows. The following is a method that throws a checked exception, and another method calls it:
[Java] View Plain copy public void storedatafromurl (string url) { try { string data = readdatafromurl (URL); } catch (badurlexception e) { e.printstacktrace (); } } Public string readdatafromurl (string URL) throws badurlexception{ if (Isurlbad (URL)) { throw new badurlexception ("bad url: " + url) ; } string data = null; return data; } Readdatafromurl () Method throws a badurlexception.。。。 Because the 11th row throws an exception, the Readdatafromurl () method must also throw a badurlexception. Otherwise, the program complains. Badurlexception is a class that I have implemented myself. Because Badurlexception inherits from Java.lang.Exception, it is checked exception:
[Java] view plain copy public class Badurlexception extends Exception {public badurlexception (String s) {super (s); If the Storedatafromurl () method wants to invoke Readdatafromurl (), it has only two choices. Either capture the badurlexception, or continue propagating the exception along the call stack. The exception was caught by Storedatafromurl () in the above code. The implementation of an upward propagation exception is as follows:
[Java] view plain copy public void Storedatafromurl (string url) throws badurlexception{string data = R Eaddatafromurl (URL); As you can see, the above code removes the catch block, plus the throws Badurlexception in the method declaration. =============================================================================================== ================ ===============================================================================
Next, discuss the implementation of the unchecked exception. First, change the badurlexception to inherit from Java.lang.RuntimeException:
[Java] view plain copy public class Badurlexception extends RuntimeException {public badurlexception (Str ing s) {super (s); } and then change the exception in the method to unchecked badurlexception:
[Java] view plain copy public void