简体   繁体   中英

catch all exceptions in the application with just one try-catch

Is there any way to catch all the excepions within an application at Main() with a single try-catch irrespective of threads and appdomains within the application??

In other words I just want to use one try-catch to log all the errors in my application instead of using multiple try catches in various places.

Would appreciate any ideas or code snippets...

Edit: I'm using a Console application and Windows Service.

Is there any way to catch all the excepions within an application at Main() with a single try-catch irrespective of threads and appdomains within the application??

No, exceptions are per thread so unless you do something to marshal exceptions from other threads to your main thread there's no way to catch exceptions from other threads.

Eg if you invoke a delegate asynchronously you need to call EndInvoke to get any exception back to the calling thread. If you just start a thread and don't do anything to handle or marshal the exception, the unhandled exception will cause the runtime to shutdown the application.

I doubt whether it would be useful to have a common place to log errors.

What do you want to achieve by this - code reduction?

Image an Exception called InvalidFileFormatException that could occur in your application whenever you try to open a file whose format is not as expected. A global exception handler could log this. Your logfile would then read something like:

[Yesterday...] The file format is invalid: InvalidFileFormatException. StackTrace: ...

But what do you gain by this information? Okay, I admit that if there's only one call in your whole application that can lead to this Exception being thrown then everything is fine. But what if there are multiple calls to the same method or if other called methods throw the same exception?

You have to rely on the Exception's detailed message, but unfortunately if it comes to Exceptions thrown by the Runtime, you cannot influence the messages. Wouldn't it be nicer to have something like

string fileName = @"C:\Users\stackoverflow\Documents\file.frk";

try
{
  FreakingObject fo = freakingObjectConverter.ReadFromFile(fileName, FreakFormat.AutoDetect);
}
catch (InvalidFileFormatException iffe)
{
  MyLogger.LogError("File " + fileName + " had an invalid format:", iffe);
}

In the example you at least get the information about the malformatted file. You could easily create more complex examples (HttpRequest etc.) where you can add very useful information to your log if only you were aware of the context the Exception was thrown in.

Small hint on the try-catch around Application.Run(...) : keep in mind that whenever you reach the catch block, your application will exit if you do not respawn the main form or do anything else.

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