Java Exceptions: Choose checked Exception or unchecked Exception?

Source: Internet
Author: User
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); }

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.