Avoid using Checked Exception in Java

xiaoxiao2021-03-06  96

This article pointed out some of the shortcomings of Checked Exception in Java, and proposes to avoid using Checked Exception in programming. For code that needs to handle Checked Exception, you can use ExceptionAdapter to pack Checked Exception. The concept of this article and ExceptionAdapter is from the Does Java Need Checked Exception of Bruce Eckel. Java's Exception is divided into two categories, and one is RuntimeException and their subclasses, and the other is Checked Exception. Java requires that the function is required to write it to the function of the function on the CatCh. However, this requirement often brings some unnecessary burdens to programmers.

In order to avoid writing the Throws part in the function declaration, you can often see the following code to 'swallow' Exception in the Java project.

Try {

// ...

} catch (exception ex) {

EX.PrintStackTrace ();

}

This is obviously not a good handling Exception method, in fact, catch and handle an Exception means recovering the program from an error (Exception). In this sense, the ongoing code can only work in some simple situations without problems.

For many Exception, there is often no way to deal with it and let the program restore from the error. The only thing to do may show some prompt information to the user on the interface. In this case, the Exception that is thrown will be thrown is a more reasonable approach. However, this will make some functions to expand sharply. A function may need to declare the 7, 8 Checked Exception, and each of the functions that call it also require the same statement.

By this is worse, this is possible to destroy the Open-Close principles designed. Simply, the Open-Close principle means that when an expanded module is expanded, it does not affect its existing client. The Open-Close principle is implemented by inheritance. When inheriting a class, we extend this class and will not affect the original client (because there is no change to this class).

Now consider the following situation, there is a parent class BASE:

Public class base {

Public void foo () throws exceptiona {

// ...

}

}

Now you need to inherit this class and overload the foo method. In the new implementation, Foo may throw ExceptionB:

Public class extend extends base {

Public void foo () throws exceptionb {

// ...

}

}

However, this is not legal inside Java because Java will see the Exception that may thrown as a part of the function characteristics, and the Exception thrown by the subclass declaration must be a subset of the parent class.

The declaration of ExceptionB can be added to the FOO method of the Base class, however, this is destroying the Open-Close principle. Moreover, sometimes we have no way to modify the parent class, such as when you overload a class in a JDK.

Another possible approach is to live in Exceptionb in the Foo method of Excend, and then construct an ExceptionA and throw it. This is a feasible way but is just a weight.

If you use RuntimeException, these problems do not exist. This shows that checked exception is not a very practical concept, which means that we should let our Exception class inherit the runtimeException instead of Exception. (This is the opposite of JDK's recommendations, but practice proves that this code is better.) For those who need to handle Checked Exception, you can use an ExceptionAdapter class to pack Checked Exception into a RuntimeException thrown. ExceptionAdapter Does Java Need Checked Excetion in Bruce Eckel, this article here, ExceptionAdapter, I have modified according to JDK 1.4:

Public class exceptionAdapter Extends runtimeException {

Public ExceptionAdapter (Exception EX) {

Super (ex);

}

Public void printstacktrace (java.io.printStream S) {

GetCause (). PrintStackTrace (s);

}

Public void PrintStackTrace (java.io.printwriter s) {

GetCause (). PrintStackTrace (s);

}

The effect of // Rethrow () is to throw the packaged exception again.

Public void rechared ()

Throws Exception

{

Throw (Exception) getcause ();

}

}

references:

Bruce Eckel - Does Java Need Checked ExceptionHttp://www.mindview.net/etc/discuss/checkedExceptions

Author: DaiJiaLin mailto: woodydai@gmail.com

http://blog.9cbs.net/daijiang

转载请注明原文地址:https://www.9cbs.com/read-107268.html

New Post(0)