简体   繁体   中英

Try-Catch not trapping exceptions

I wrote a simple console app in Visual Studio 10 on a Windows 7 32 bit machine using all managed code. It works fine in debug mode, but when I deploy it to another machine and run it, I get the message,

[my assembly] has encountered a problem and needs to close.  We are sorry for the inconvenience.

... and then it generates a cryptic error report that I cannot imagine ever finding useful. Here's the thing: the exception is occurring inside a try-catch statement that is designed to gracefully handle the error; the exception does not seem to be caught, however.

How can this be? How can I fix it?

Either the exception is thrown from a diferent place in the code, or you do not list the particular type of the exception (or one of its parent types) in the catch branches

The "cryptic" error message actually is designed to help you find out: what went wrong (the type of the exception), where did it occur (stack trace). You should learn to read/"decypher" it

Well like Attila said, it's probably being thrown from another place. You should set the type to Exception as it is general and if the error is being thrown where you think it is then it definitely will end up in that catch. You can adjust the type to something more specific once the problem is understood.

Also instead of handling it gracefully, first try to print the error message to the console to see what the problem could be.

If it is not being caught it's not being thrown from inside that try , make sure that all the code in question is wrapped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM