Try-catch: is this acceptable practice?

前端 未结 8 2128
终归单人心
终归单人心 2020-12-17 17:35

We have received Java code from a software supplier. It contains a lot of try-catch blocks with nothing in the catch part. They\'re all over the pl

相关标签:
8条回答
  • 2020-12-17 18:11

    It's not the best:

    • It hides evidence of the exception so debugging is harder

    • It may cause features to fail silently

    • It suggests that the author might actually have wanted to handle the exception but never got around to it

    So, there may be cases where this is OK, such as an exception that really is of no consequence (the case that comes to mind is Python's mkdirs, which throws an exception if the directory already exists), but usually, it's not so great.

    0 讨论(0)
  • 2020-12-17 18:13

    Unfortunately you cannot just remove it, because it the try block throws a checked exception then it will have to be declared in the throws clause of the method. The callers of the method will then have to catch it (but not if the callers also have this catch (Exception e) {} abomination).

    As an alternative, consider replacing it with

    try {
        meshContinuum.enable(MeshingModel);
    } catch (Exception e) {
        throw (e instanceof RuntimeException) ? (RuntimeException) e : new RuntimeException(e);
    }
    

    Since RuntimeException (and classes that extend it) are unchecked exceptions they do not need to be declared in the throws clause.

    0 讨论(0)
  • 2020-12-17 18:15

    Terrible, indeed. Swallowing an exception like this can be dangerous. How will you know if something bad has happened?

    I'd feel better if the vendor wrote comments to document and acknowledge it ("We know what we're doing"). I'd feel even better if there was a strategy apparent in the code to deal with the consequences. Wrap it in a RuntimeException and rethrow; set the return value to an appropriate value. Anything!

    "All over the place"? Are there multiple try/catch blocks littering the code? Personally, I don't like that idiom. I prefer one per method.

    Maybe you should find a new vendor or write your own.

    0 讨论(0)
  • 2020-12-17 18:15
        try {
            meshContinuum.enable(MeshingModel);
        } catch (Exception e) {
        }
    

    This looks like unfinished code. If the enable method throws an Exception then it will be caught and swallowed by the code. If it doesn't then it does not make sense to try to catch a non occuring exception.

    Check to see the methods and where their signatures are not followed by throws exceptionName, then remove the empty try-catch statements from the places they are called.

    You can theoretically put try-catch around any statement. The compiler will not complain about it. It does not make sense though, since this way one may hide real exceptions.

    You can see this as a sign of bad code quality. You should probably be prepared to run into problems of different type too.

    0 讨论(0)
  • 2020-12-17 18:27

    What did your contract with the supplier specify? If what they wrote, bad practice and all, meets the spec then they will charge you for a rewrite.

    Better to specify a set of tests that will enter many or all of those try-catch blocks and hence fail. If the tests fail you have a better argument to make them fix their terrible code.

    0 讨论(0)
  • 2020-12-17 18:28

    Actually ... not so fast.

    There are legitimate cases for ignoring an exception.

    • Suppose that an exception has happened already, and we're already in a catch(). While in the catch(), we want to make best effort to clean up (which could fail, too). Which exception to return?? The original one, of course. The code looks like this:
        try {
            something-that-throws();
        } catch(Exception e) {
            try {
                something-else-that-throws();
            } catch(Exception e1) {}
            throw e;
        }
    
    • When we really don't care whether an operation succeeds, but (unfortunately) the operation's author throws an exception if a failure occurs.
        if (reinitialize) {
            try {
                FileUtils.forceDelete(sandboxFile); // delete directory if it's there
            } catch(Exception e) {}
        }
    

    The above saves a rather tortured attempt to see if sandboxFile actually exists before deleting it anyway.

    0 讨论(0)
提交回复
热议问题