473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

What Exceptions to catch

Hi,

I need to catch exceotions on File.Delete()

After checking the help, I have noticed that thgere are serevral
Exceptions that can be thrown.
My question is, should I catch all thes Exceptions, or if I simply do
the following:

try
{

}

catch(Exception e)
{
e.Message; //do something useful with this
}

Will this catch all the Exceptions that a File.Delete can throw ?

Regards,

Steven
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
7 3002
Hi,

If all you're doing insinde try {} catch is File.Delete, do a
"catch(exception e)", this will capture all events that could be raised.
System.Exception is the mother of all exceptions and will thus catch all
possible exceptions.

Erik

"Steven Blair" <st**********@btinternet.com> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hi,

I need to catch exceotions on File.Delete()

After checking the help, I have noticed that thgere are serevral
Exceptions that can be thrown.
My question is, should I catch all thes Exceptions, or if I simply do
the following:

try
{

}

catch(Exception e)
{
e.Message; //do something useful with this
}

Will this catch all the Exceptions that a File.Delete can throw ?

Regards,

Steven
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Hi,

http://msdn.microsoft.com/library/de...eleteTopic.asp

here are listed all the exceptions it can raise. Sure, with Exception you
can catch them all, but if you need to do specific processing based on
exception type, having more than one catch clause is an option.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Steven Blair" <st**********@btinternet.com> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hi,

I need to catch exceotions on File.Delete()

After checking the help, I have noticed that thgere are serevral
Exceptions that can be thrown.
My question is, should I catch all thes Exceptions, or if I simply do
the following:

try
{

}

catch(Exception e)
{
e.Message; //do something useful with this
}

Will this catch all the Exceptions that a File.Delete can throw ?

Regards,

Steven
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Hi steven,
Will this catch all the Exceptions that a File.Delete can throw ?


It will do that, if all you need to know is if was a problem deleting the
file that's the way to go, if you need to do different things depending of
why it fails you should catch the particular exception, in this case you can
declare for example two catch one for the exception that you are interested
in and other for Exception to catch all the others.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #4
Ok thanks,

Thats definately made things a bit clearer for me now :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Steven:

I would highly recommend against catching System.Exception except in very
rare situations. Exception handling isn't meant to simply keep the app
from crashing, it's purpose is to allow you to respond to predictable
problems in a structured method. For instance, if you attempt to open a DB
connection, the server could be down, network cable unplugged etc. None of
which is the fault of your code. As such, you would probably want to catch
a SqlException for instance and respond in a special way .

I see Ignacio posted the possible exceptions, but trap precisely. If you
use System.Exception, and an OutOfMemoryException is raised, it'll be
caught. If you catch System.Exception you could easily hide problems and
introduce logic errors which are very very bad.

HTH,

Bill
"Steven Blair" <st**********@btinternet.com> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hi,

I need to catch exceotions on File.Delete()

After checking the help, I have noticed that thgere are serevral
Exceptions that can be thrown.
My question is, should I catch all thes Exceptions, or if I simply do
the following:

try
{

}

catch(Exception e)
{
e.Message; //do something useful with this
}

Will this catch all the Exceptions that a File.Delete can throw ?

Regards,

Steven
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6
There should be a proper Hierarchy in Exception handling.
All the Exceptions which can occur due to a statement or block of code
should be handled first, after catching and handling all such
predicted exceptions, we should go higher in hierarchy to catch
unpredicted exceptions.

Unpredicted exceptions should also be handled for logging and
debugging purposes.

Also note there is difference between a bare catch and catch(Exception
e) statements.

catch(Exception e) will catch only those exceptions defined under
System whereas a bare catch will catch all exceptions (might be
generated in some com component for example).

Hope it will help.

--
Cheers,
Rahul Anand
"William Ryan eMVP" <bi**@NoSp4m.devbuzz.com> wrote in message news:<#T**************@TK2MSFTNGP11.phx.gbl>...
Steven:

I would highly recommend against catching System.Exception except in very
rare situations. Exception handling isn't meant to simply keep the app
from crashing, it's purpose is to allow you to respond to predictable
problems in a structured method. For instance, if you attempt to open a DB
connection, the server could be down, network cable unplugged etc. None of
which is the fault of your code. As such, you would probably want to catch
a SqlException for instance and respond in a special way .

I see Ignacio posted the possible exceptions, but trap precisely. If you
use System.Exception, and an OutOfMemoryException is raised, it'll be
caught. If you catch System.Exception you could easily hide problems and
introduce logic errors which are very very bad.

HTH,

Bill

Nov 16 '05 #7

"Rahul Anand" <ra************@rediffmail.com> wrote in message
news:62**************************@posting.google.c om...
There should be a proper Hierarchy in Exception handling.
All the Exceptions which can occur due to a statement or block of code
should be handled first, after catching and handling all such
predicted exceptions, we should go higher in hierarchy to catch
unpredicted exceptions.

Unpredicted exceptions should also be handled for logging and
debugging purposes.

Also note there is difference between a bare catch and catch(Exception
e) statements.

catch(Exception e) will catch only those exceptions defined under
System whereas a bare catch will catch all exceptions (might be
generated in some com component for example).

Hope it will help.

--
Cheers,
Rahul Anand


To be precise, using a bare catch will catch non-cls compliant exceptions
(exceptions which do not derive from System.Exception, such as integers
instead of objects), and using catch(System.Exception) will catch all
cls-compliant exceptions. From what I've read if you write CLS compliant
code then you should never get a non-cls compliant exception - even COM and
win32 exceptions are translated into cls compliant exceptions.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
22
by: Drew | last post by:
How do I know which exceptions are thrown by certain methods? For example, reading a file might throw an IO Exception, etc. In Java, the compiler won't even let you compile unless you put your...
9
by: Steven Blair | last post by:
Hi, I need to catch exceotions on File.Delete() After checking the help, I have noticed that thgere are serevral Exceptions that can be thrown. My question is, should I catch all thes...
6
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
16
by: Einar Høst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some...
9
by: Jeff Louie | last post by:
I drank too much coffee last night and came up with a suggestion about how to add checked exceptions to C# http://www.geocities.com/jeff_louie/OOP/oop14.htm Comments expected <g> Regards,...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
3
by: Robert Rotstein | last post by:
It appears that exception handling at the top-most level of a C# program, in the static void Main() method, differs depending on whether the program is run in debug mode or not. That is, code such...
2
by: Eric Sabine | last post by:
I built a generic exception handler form which allows the user to get information from it, print it, email it, etc. for exceptions for which I explicitly didn't handle in code, such as missing...
11
by: Howard Kaikow | last post by:
I'm using the code below, but an error is not getting trapped. Where can such errors occur? In another process? Try SomeCode Catch ex As Exception Dim strMsg() As String = Split(ex.ToString,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.