Hi All,
I have been bomb proofing a bit code today, I have found the try catch statement does it nicely I did have the catch loop as below to tell me something had blown up and I could then track down what had pooped and sort it as below:
catch
{
MessageBox.Show("ERROR");
}
but investigation led nowhere so I commented out the MessageBox and problem solved or masked over? I will run it on a different PC to see if this has cured the fault, if it has how?
Glenn
Loading

VulpesRank #55 · 29.2K reputationPosted Mar 7, 2012, 7:09 AM EST
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Sometimes an exception is non-fatal and you can simply 'swallow' it and carry on.
However, it's unwise to do this without knowing what caused the exception in the first place. It may come back and bite you (or, worse still, your client) later.
A good strategy is to check the MSDN docs to see what exceptions can be thrown by the various .NET Framework methods and properties and try to catch those exceptions specifically.
Glenn PattonRank #6,159 · 275 reputationPosted Mar 7, 2012, 7:22 AM EST
Glenn PattonRank #6,159 · 275 reputationPosted Mar 8, 2012, 12:57 AM EST
MessageBox.Show(ex.Message);
Is not displaying anything have I swallowed it?
Glenn
VulpesRank #55 · 29.2K reputationPosted Mar 8, 2012, 1:11 AM EST
Perhaps there's no exception being thrown?
To make absolutely sure there's nothing funny going on, try this 'belt and braces' approach:
try
{
// code}
}
catch(Exception ex)
{
if (!String.IsNullOrEmpty(ex.Message))
{
MessageBox.Show(ex.Message);
}
else
{
MessageBox.Show("an exception occurred with no message");
}
}
catch
{
MessageBox.Show("A 'funny' exception occurred");
}
Glenn PattonRank #6,159 · 275 reputationPosted Mar 8, 2012, 2:05 AM EST
Glenn
VulpesRank #55 · 29.2K reputationPosted Mar 8, 2012, 3:16 AM EST
So, if the MessageBox is still not popping up, then no exception is being thrown.
Glenn PattonRank #6,159 · 275 reputationPosted Mar 8, 2012, 3:28 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 8, 2012, 5:00 AM EST
So, if there's no exception in the try block , the code in the catch clause(s) will not execute and execution will pass to the following statement in the normal way.
Consequently, as we've written the code in such a way that it will catch ALL exceptions and print some message, then if no message pops up, there's no exception being thrown.
Glenn PattonRank #6,159 · 275 reputationPosted Mar 8, 2012, 5:29 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 8, 2012, 5:53 AM EST
Sometimes you can implement a 'retry' strategy when an intermittent fault occurs, particularly if it's hardware related and the user may be able to do something about it before trying again.
The basic code for 'retry' is:
Glenn PattonRank #6,159 · 275 reputationPosted Mar 8, 2012, 6:15 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 8, 2012, 6:19 AM EST
Can you keep on trying whatever it is you're doing until eventually an exception is thrown?
Glenn PattonRank #6,159 · 275 reputationPosted Mar 8, 2012, 6:24 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 8, 2012, 6:49 AM EST
But, in my experience, clients don't mind the odd error if it's handled gracefully. What they don't like is when the app blows up with no opportunity for remedial action.
Glenn PattonRank #6,159 · 275 reputationPosted Mar 9, 2012, 12:19 AM EST