I know try catch have been discussed a lot but I haven’t found a solution to my problem yet.
I’m writing a Silverlight application where every exception should generate a MessageBox that says something like “Sorry of the inconvenience”.
Since I cannot guarantee that my code will be free from exceptions my coworker has instructed me to have a try catch in every method (a couple of hundred) like this:
public void Method1()
{
try
{
...
}
catch (Exception e)
{
MessageBox.Show("Something went wrong, we apologize for the inconvenience. \n" + e.Message);
}
}
public void Method2()
{
try
{
...
}
catch (Exception e)
{
MessageBox.Show("Something went wrong, we apologize for the inconvenience. \n" + e.Message);
}
}
But it seems so excessive. I’ve read that one does not use try catch in this way plus there will be a lot of duplicated code plus the code will be obfuscated and hard to read.
Are there any alternatives like a global try catch I can use?
you can always handle the AppDomain.UnhandledException Event