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
VulpesPosted Mar 7, 2012, 12:09 PM
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 PattonPosted Mar 9, 2012, 5:19 AM
VulpesPosted Mar 8, 2012, 11:49 AM
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 PattonPosted Mar 8, 2012, 11:24 AM
VulpesPosted Mar 8, 2012, 11:19 AM
Can you keep on trying whatever it is you're doing until eventually an exception is thrown?
Glenn PattonPosted Mar 8, 2012, 11:15 AM
VulpesPosted Mar 8, 2012, 10:53 AM
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 PattonPosted Mar 8, 2012, 10:29 AM
VulpesPosted Mar 8, 2012, 10:00 AM
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 PattonPosted Mar 8, 2012, 8:28 AM
VulpesPosted Mar 8, 2012, 8:16 AM
So, if the MessageBox is still not popping up, then no exception is being thrown.
Glenn PattonPosted Mar 8, 2012, 7:05 AM
Glenn
VulpesPosted Mar 8, 2012, 6:11 AM
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 PattonPosted Mar 8, 2012, 5:57 AM
MessageBox.Show(ex.Message);
Is not displaying anything have I swallowed it?
Glenn
Glenn PattonPosted Mar 7, 2012, 12:22 PM