Category: Java (35)
Directory (?) [+]
Java contains two exceptions:checked exceptions and unchecked exceptions . C # has only unchecked exceptions. The difference between checked and unchecked exceptions is:
Checked exceptions must be explicitly captured or passed, as described in basic try-catch-finally Exception handling. Unchecked exceptions can be not captured or thrown. The checked exception inherits the Java.lang.Exception class. The unchecked exception inherits from the Java.lang.RuntimeException class.
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; //read lots of data over http and return //it as a String instance. The return data; } Readdatafromurl () method throws the Badurlexception. 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 Storedatafromurl (string url) {String data = Readdatafromurl (URL); }