Try-catch: is this acceptable practice?

前端 未结 8 2139
终归单人心
终归单人心 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: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.

提交回复
热议问题