Exceptions handling

  • Thread starter Thread starter ___Newbie___
  • Start date Start date
N

___Newbie___

Hello,

Does every error encountered during try() block is handled by
catch(Exception e) ? Do I have to catch every aspect or every detail of
exception? e.g. catch (IOException io) catch (OutOfMemory mem)exception..
What I'm after is a generic catch().
Thanks
 
Does every error encountered during try() block is handled by
catch(Exception e) ? Do I have to catch every aspect or every detail of
exception? e.g. catch (IOException io) catch (OutOfMemory mem)exception..
What I'm after is a generic catch().

catch(Exception e) catches all exceptions. You might want to read up on
"finally" as well.

Here is a tutorial on exception handling:
http://www.programmersheaven.com/2/les_csharp_9_p1

Laban
 
Hello ___Newbie___" d,

Read this article

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp

_> Hello,
_>
_> Does every error encountered during try() block is handled by
_> catch(Exception e) ? Do I have to catch every aspect or every detail
_> of
_> exception? e.g. catch (IOException io) catch (OutOfMemory
_> mem)exception..
_> What I'm after is a generic catch().
_> Thanks
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
You can catch all exceptions by specifying catch(Exception...) however, even
though this is used extensively, it isn't reccommended practice. You can
expect certain sorts of exceptions, for example, if you're doing file
operations you may expect an io exception and wish to handle it in a
different way. In this case you can use catch blocks for all of the
exception types you expect to see and handle in a different way.

That being said, a generic catch of an exception is the lazy way out and if
you only have yourself to please then just go ahead...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
I came up with a rather novel approach to this, which we use in our apps. We
have a static error handler which identifies the type of Exception, and
depending upon the Exception type, extracts specific information from it for
loggin purposes. We have methods that work individually with various
Exception types, to extract the data from them and append to a
StringBuilder. The static Exception handler calls the appropriate method for
extracting the particular Exception type, and will call itself recursively
for any inner Exceptions. As a result, we get some good logging regarding
Exceptions. Now, this Exception handler can either rethrow the Exception, or
ignore it, in case a client app needs to do more with it. But the logging is
an invaluable diagnostic benefit.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Back
Top