HackerTrans
TopNewTrendsCommentsPastAskShowJobs

wallyhs

no profile record

comments

wallyhs
·11년 전·discuss
> You have to assume that code cleans itself up as the stack unwinds.

Exceptions or not, if you have an unknown error condition, how can you assume anything?

I apologize for the delayed response.
wallyhs
·11년 전·discuss
That's my point: in this situation, you can't display an error and keep going. How do you know that you are not in this situation when you catch an unknown exception?
wallyhs
·11년 전·discuss
You can only recover from such errors locally. The local context is not available in a global exception handler.
wallyhs
·11년 전·discuss
> Corruption is outside the state of this discussion.

Why? If corruption leads to an exception, it seems relevant. Corruption could be the application's own fault, it could be failing hardware, it could be someone trying to exploit a vulnerability. It could be anything - a result of undefined behavior. Sorry if I'm using the term loosely.

> If half your app just overwrote the other half of your app, no matter what we say here is going to make any difference.

If it overwrote the other half, it should crash immediately. I can't just display an error message and retry the operation in that case. If I don't know what exception I'm handling, how can I be sure that I won't cause more damage by handling it?

> The other part (and in my opinion the important part) of exception handling is ensuring your stack unwinds correctly.

That is not what I was referring to. A program can still get into an inconsistent state with exceptions. For example, if a function updates half of some data structure and then throws, that data structure may be left in an inconsistent state. Of course you should not write functions that way, but it is an easy mistake to make. Do you really want your app to keep running in an inconsistent state?

I'm looking at it from a defensive, assume-the-worse perspective. If my application is in an inconsistent state, it should cease running immediately. If my application does not know which state it is in, it must assume that it is in an inconsistent state.
wallyhs
·11년 전·discuss
> No, you shouldn't. What are you going to do any differently with that than you would FileIsLockedException or NetworkIsDownException or HarddriveIsCorruptException? You wouldn't do anything differently. Show the user the ErrorMessage in the dialog and allow them to restart whatever operation was in affect. Let them choose a shorter path, close Excel, plug their Ethernet cable back in, or whatever. Trying to play wack-a-mole is a fools errand.

I don't understand this viewpoint. If a file is locked, the user may be able to fix it, so go ahead and display an error. But if there is corruption, then what is the user going to do? You can't tell them, "Restore your system from backup, and then click OK." Your program has to crash before it causes more problems.

What if the situation is stack corruption or heap corruption? Or what if the error left the application in an inconsistent state? Continuing to run might corrupt the user's data. What if an attacker caused the corruption in order to gain unauthorized access? Logging or displaying the error might be exactly what they want.

I often see variations of this advice: If you don't know how to handle it, don't catch it. By definition, you don't know how to handle an unknown exception.

> Put MSDN away, you don't need it.

I think it is a good idea to understand the behavior and failure modes of every API function that you call; this requires that you consult the documentation.