Why do good programmers sometimes silently swallow

2019-03-08 23:54发布

I know it's evil, but I've seen swallowed exceptions in code written by a good programmer. So I'm wondering if this bad practice could have at least one positive point.

In other words, it is bad but why do good programmers, on rare occasions, use it?

try
{
    //Some code
}
catch(Exception){}

19条回答
一夜七次
2楼-- · 2019-03-09 00:24

Because sometime even good programmers make mistakes.

Either that or your opinion of a good programmer isn't the same as some (because a good programmer would have left some reasoning behind as to why the Exception was swallowed rather than something being done with the information).

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-09 00:24

Mostly because they are too tired. Same reason as they forget to do documentation some of the time.

查看更多
甜甜的少女心
4楼-- · 2019-03-09 00:25

I do it when an exception can't be handled effectively, it shouldn't impact the normal operation of the application, and/or it might represent a transient condition that is known but has little overall impact.

A simple example is logging. You don't need the app to blow up just because some logging information won't be saved to your backing store.

Another example might be where you have alternative means of handling the situations, like this:

private Boolean SomeMethod() {
   Boolean isSuccessful = false;
   try {
      // call API function that throws one of several exceptions on failure.
      isSuccessful = true;
   } catch(Exception) { }

   return isSuccessful;
}

In the above example, you may not have a fall back position, but you don't want it to filter up. This is ok for SHORT methods where the return value is only set to a successful status as the last step in the try block.

Unfortunately there are many calls that will throw exceptions instead of simply returning an error code or false condition in the result. I've also seen calls that throw more exceptions than what their documentation suggests which is one of the reasons why I'll put a catch all around them.

查看更多
ら.Afraid
5楼-- · 2019-03-09 00:26

at the very least write it to the output window

try
{ }
catch (exception ex)
{
System.Diagnostics.Debug.Writeline(" exception in method so and so : " ex.message);
}
查看更多
别忘想泡老子
6楼-- · 2019-03-09 00:28

Without claiming to be a good programmer, I can at least explain why I sometimes catch and ignore. There are times when what I'm working on has to deal with an exception to compile, but is not the appropriate place to actually handle the exception. For instance, I was recently working in a program which parses a good deal of XML as one of its tasks. The actual XML parser opened a file, which generated an exception. The XML parser was not the best place to generate a dialog informing the user of bad file choice, though. Rather than get sidetracked with throwing the exception to the right place and handling it there, I silently caught the exception, leaving a comment and TODO (which my IDE tracks for me). This allowed me to compile and test without actually handling the exception. Sometimes, I will forget to take care of the TODO immediately, and the swallowed exception sticks around for a while.

I know this is not the best programming practice, and I certainly am not an expert programmer, but I think this is an example of why someone would want to do this.

查看更多
成全新的幸福
7楼-- · 2019-03-09 00:31

Because something has to be finished for a presentation the boss spontaneously does tomorrow and nothing is worse than the program crashing or showing a nasty error message, while when something is just not working he can always "talk around" that.

After the presentation, no one will improve those "quick fixed" parts of course. ;-)

查看更多
登录 后发表回答