Is there any valid reason to ever ignore a caught exception

后端 未结 24 1748
南笙
南笙 2020-11-27 15:43

Wow, I just got back a huge project in C# from outsourced developers and while going through my code review my analysis tool revealed bunches of what it considered bad stuff

相关标签:
24条回答
  • 2020-11-27 16:09

    One example of where I'd consider this acceptable is in some non-critical module of a critical application (say, in the sound feedback module of the space shuttle navigation system), for exceptions that should never happen, and cannot be handled more cleanly.

    In those cases, you would not want to let that exception propagate and cause the whole application to fail (Sorry guys, no more navigation system, our beeping module crashed, and there was really nothing we could do about it).

    Edited to say that in any of these cases, you'd at least want to log the event somewhere.

    0 讨论(0)
  • 2020-11-27 16:10

    I think the best rule of thumb is only ignore an exception if you're completely aware of what the exception means and the possible ramifications of it. In the case of some isolated module that doesn't affect the rest of your system I think it would be okay to just catch the generic Exception as long as you know nothing bad happens to anything else.

    IMO it's easier to know the ramifications in Java since each method is required to declare all exceptions it can throw so you know what to expect, but in C# an exception can be thrown even if it isn't documented, so it's hard to know all the possible exceptions that can be thrown by a method, and lacking that knowledge it is usually a bad idea to catch all.

    0 讨论(0)
  • 2020-11-27 16:11

    That's a really bad thing to be doing.

    While there are valid reasons you might want to ignore exceptions - if it's expected in some way, and there's no need to do anything about it - however doing it 2000 times seems like they just want to sweep their exceptions under the rug.

    Examples of where it's OK to swallow exceptions might be probing for things... you send off a message to some device or module, but you don't care if it actually gets there.

    0 讨论(0)
  • 2020-11-27 16:12

    Yes, its acceptable (unavoidable, necessary) in certain circumstances per Maxim's post. That doesnt mean you have to like it. 2106 violations is probably WAY too many and at the least they should have added a comment in the catch block as to why it was ok to swallow this exception.

    @Dustin Its bad practice to show any exception details to a public user (stack traces, line numbers, etc). You should probably log the exception and display a generic error.

    0 讨论(0)
  • 2020-11-27 16:13

    The other case where you can be excused for catching and ignoring exceptions is when you're unit testing.

    public void testSomething(){
        try{
            fooThatThrowsAnException(parameterThatCausesTheException);
            fail("The method didn't throw the exception that we expected it to");
        } catch(SomeException e){
            // do nothing, as we would expect this to happen, if the code works OK.
        }
    }
    

    Note that, even though the catch block does nothing, it explains why.

    Having said this, more recent testing frameworks (Junit4 & TestNG) allow you to specify the exception that is expected - which leads to somthing like this...

    @Test(expected = SomeException.class)
    public void testSomething(){
        fooThatThrowsAnException(parameterThatCausesTheException);
        fail("The method didn't throw the exception that we expected it to");
    }
    
    0 讨论(0)
  • 2020-11-27 16:13

    Think of it this way - if you are expending the CPU cycles to catch the exception but then swallowing, you are ignoring a potential issue and wasting CPU. As many have said the application should not be throwing that many exceptions unless you have something poorly constructed.

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