Im trying to implement a global "try catch block" and keep my app running after handling any:
public class GlobalExceptionHandler
{
public GlobalExceptionHandler()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
throw (e.ExceptionObject as Exception);
}
catch (Exception ex)
{
}
}
}
Problems:
- I expected to handle all exceptions in a familiar way with a
try
catch
block. Am i doing smth wrong by rethrowing them to catch? - The code above still drops the app with an exception popup in the place exception was originally thrown like it has not been handled.
- Guess my approach is a complete fail from the start. I wanted to avoid using a lot of
try
catch
blocks through the whole app and decided to localize all exceptions handling. How good this idea is? Should i find a better way to implement a global handler or stick to dozens oftry
catch
blocks?
Aucun commentaire:
Enregistrer un commentaire