简体   繁体   中英

Java Exception Handling - is this good practice?

I am working on a Java plugin for a framework.

I have written my code in such a way that entryPoint function looks like below (consider this is the starting point, main function)

function entryPoint()
{
   try{
      //some code block
      subFunction1();
      subFunction2();
   }
   catch(Exception e) {}
   catch(IOException ioe) {}
   catch(NullPointerException npe){}
}

function subFunction1() throws IOException
{
    //some code
}

function subFunction2() throws NullPointerException
{
    //some code
}

So the idea is, all the sub functions throws specific exceptions to major function and we catch these exceptions in the major functions and do handling.

Is this way correct? If not please suggest better way.

  • The order of the catch statements should be changed. Since the first catch will match all Exceptions , the following two will never be triggered.

  • An NPE is in most cases unexpected and not recoverable. Catching it implies that the application is able to recover from it and run regardless. Is it really the case?

  • Even if the NPE is recoverable, it is a better practice to check for != null instead of relying on exceptions for command flow. This is for conceptual reasons (exception-based command flow requires more code, is less readable, the intention is often unclear) as well as for performance reasons.

  • All Exceptions are swallowed - no logging or rethrowing happens. This way, no one will know if and when something goes wrong because there are no exceptions logged. In most cases, users, other developers and maintainers expect almost all exceptions to be truly exceptional and therefore logged.

不要捕获或抛出NullPointerException,而Exception的catch块应该是最后一个

I think this approach is perfect. One should try to handle specific exception rather handling all in one. Putting a try-catch block just for the heck of it is not use of it, but abuse of try-catch thing.

Yeah Exception should be the last one to be handled.Missed this.

In short your intent is good and syntactically you can depend on compiler.

You should do the exception-handling the same way you would do it, if it was no plugin. It depends on you application, if you can handle all exceptions in one major method. If there are case, in which you can continue work, than this could be difficult this way.

The only thing that I would do regarding the plugin-thing is an all surrounding 'catch-all' and maybe some special cases to do some more detail logging. And this also has just to be done, if the framework doesn't do it itself.

I think it is best to catch an exception at the first possible place where you can actually resolve the resulting problem.

An example:

function int divide(int a, int b) throws DivisionByZeroException {
    if(b == 0){
        throw new DivisionByZeroException();
    }
    return a / b;
}

function int doCalculationsAndStuff(int a, int b) throws DivisionByZeroException {
    int result = divide(a, b);
    ...
    return result; 
}

function void main() {
    try {
        int a = userInput();
        int b = userInput();
        int result = doCalculationsAndStuff(a, b);
        print("result: " + result);
    } catch(DivisionByZeroException e) {
        print("Division by zero happened. But i catched it for you =).");
    }
}

It wouldn't make sense to handle the exception in divide() or doCalculationsAndStuff() . Because what value would you return in case of a division by zero? Nothing, right that's why we throw the exception and handle it in the main function where we give the user of our calculator app some feedback.

Now back to your question. If the entryPoint function is the first place where you can resolve the problems that occur in your subfunctions than thats a good place to handle them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM