473,378 Members | 1,456 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,378 software developers and data experts.

Continue execution after exception?

Hi,

I'm writing a simple application that deletes multiple directories. I want
to write it so that it will simply continue on through the list of
directories if an exception is thrown (the directory is not there). Is there
a way to do this? Am I approaching this all wrong?
Nov 22 '05 #1
5 13712
You can use Directory.Exists(path) to test if a directory exists before
trying to access objects in it, thereby avoiding an exception in the 1st
place.
Other then that, you can use...
foreach ( string directory in Directory.GetDirectories(filter) )
{
try
{
// code that accessses the directory
}
catch(Exception ex)
{
// this output is optional - there are many other things you can do here
Debug.WriteLine("Error accessing directory.\n" + ex.Message);
}
}

and execution will continue after spitting out the debug trace.

I suggest NOT doing it this way...the real question is what would you be
doing that incurs an exception? If you can avoid it you are better off doing
so.

"Antipode" <An******@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Hi,

I'm writing a simple application that deletes multiple directories. I want to write it so that it will simply continue on through the list of
directories if an exception is thrown (the directory is not there). Is there a way to do this? Am I approaching this all wrong?

Nov 22 '05 #2
You can use Directory.Exists(path) to test if a directory exists before
trying to access objects in it, thereby avoiding an exception in the 1st
place.
Other then that, you can use...
foreach ( string directory in Directory.GetDirectories(filter) )
{
try
{
// code that accessses the directory
}
catch(Exception ex)
{
// this output is optional - there are many other things you can do here
Debug.WriteLine("Error accessing directory.\n" + ex.Message);
}
}

and execution will continue after spitting out the debug trace.

I suggest NOT doing it this way...the real question is what would you be
doing that incurs an exception? If you can avoid it you are better off doing
so.

"Antipode" <An******@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Hi,

I'm writing a simple application that deletes multiple directories. I want to write it so that it will simply continue on through the list of
directories if an exception is thrown (the directory is not there). Is there a way to do this? Am I approaching this all wrong?

Nov 22 '05 #3
Catch and discard the exception. At its simplest:

try
{
// do your stuff here.
}
catch ( SomeExpectedTypeOfException e )
{
// but don't do anything with it.
}

It would be wise to explicitly catch and discard only the specific exception
types you're willing to ignore. Other types of exceptions might be
legitimate, and you may want to catch them as well and handle them in other
ways (a Permissions exception, for instance, is a show-stopper). But there's
no reason why you can't continue processing in your method, regardless of
the exceptions you catch, as long as you don't rethrow the exception in the
catch block. Remember to order your catch clauses in order of most-specific
exception type to least specific.

HTH,
Tom Dacon
Dacon Software Consulting

"Antipode" <An******@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Hi,

I'm writing a simple application that deletes multiple directories. I want to write it so that it will simply continue on through the list of
directories if an exception is thrown (the directory is not there). Is there a way to do this? Am I approaching this all wrong?

Nov 22 '05 #4
Catch and discard the exception. At its simplest:

try
{
// do your stuff here.
}
catch ( SomeExpectedTypeOfException e )
{
// but don't do anything with it.
}

It would be wise to explicitly catch and discard only the specific exception
types you're willing to ignore. Other types of exceptions might be
legitimate, and you may want to catch them as well and handle them in other
ways (a Permissions exception, for instance, is a show-stopper). But there's
no reason why you can't continue processing in your method, regardless of
the exceptions you catch, as long as you don't rethrow the exception in the
catch block. Remember to order your catch clauses in order of most-specific
exception type to least specific.

HTH,
Tom Dacon
Dacon Software Consulting

"Antipode" <An******@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Hi,

I'm writing a simple application that deletes multiple directories. I want to write it so that it will simply continue on through the list of
directories if an exception is thrown (the directory is not there). Is there a way to do this? Am I approaching this all wrong?

Nov 22 '05 #5
Handle exceptions in a loop (like for each or so)...so after the exception is
caught and you handle it, the next iteration would start and it would go on
and on....

"Antipode" wrote:
Hi,

I'm writing a simple application that deletes multiple directories. I want
to write it so that it will simply continue on through the list of
directories if an exception is thrown (the directory is not there). Is there
a way to do this? Am I approaching this all wrong?

Nov 22 '05 #6

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

Similar topics

3
by: Colin McKinnon | last post by:
Hi All, I'm trying to work out how to close the connection to the browser at a defined point within my code, but go on to do some time-consuming processing with PHP. Although it *should* be...
2
by: Michael Satterwhite | last post by:
I *MUST* be overlooking something obvious. Consider the following code: foreach($_POST as $key=>$value) { print "$key=>$value<br />"; if(! empty($value)) { switch($key) { case "Submit": case...
4
by: Richard Lewis | last post by:
Hi there, Is it possible to have an 'except' case which passes control back to the point after the exception occurred? e.g. # a function to open the file # raises FileLockedException is...
3
by: Antipode | last post by:
Hi, I'm writing a simple application that deletes multiple directories. I want to write it so that it will simply continue on through the list of directories if an exception is thrown (the...
13
by: tolisss | last post by:
Hi i have setup a global exception handler b4 Application.Run like Application.ThreadException += new ThreadExceptionEventHandler(GlobalExceptionProcessing.AppThreadException ); then after...
6
by: Mohan | last post by:
Hi, I am learning the Exception Handling in C++. I wrote a small program using Exception Handling. I am using Vistual Studio 6. It is working fine in Win32 Debug build, but it is not...
1
by: a.mustaq | last post by:
Hi Guys, I am developing a asp.net application.In this I have to handle exceptions in application level. When ever an an exception occurs in any part of the application it should be hanldeld at...
0
by: PyNoob | last post by:
By default, uncaught exceptions stop the execution of python scripts (similar to what bash does when -e us set.) I need to change this behavior, so that the user gets informed about the uncaught...
3
by: E. Kwong | last post by:
I have a For loop to insert record to a SQL server database, like: For Each item As ListItem In cblxyz.Items If (item.Selected) Then .... do something Try srcxyz.Insert() Catch SQLExp As...
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.