473,950 Members | 36,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13748
You can use Directory.Exist s(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.GetDi rectories(filte r) )
{
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******@discu ssions.microsof t.com> wrote in message
news:05******** *************** ***********@mic rosoft.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.Exist s(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.GetDi rectories(filte r) )
{
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******@discu ssions.microsof t.com> wrote in message
news:05******** *************** ***********@mic rosoft.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 ( SomeExpectedTyp eOfException 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******@discu ssions.microsof t.com> wrote in message
news:05******** *************** ***********@mic rosoft.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 ( SomeExpectedTyp eOfException 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******@discu ssions.microsof t.com> wrote in message
news:05******** *************** ***********@mic rosoft.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
6953
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 possible to do this by moving the slow stuff into a function registered with register_shutdown_function, it appears that in many cases, the connection is not closed before calling the shutdown function - there are also issues with calling other...
2
2705
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 "keyList": case "curKey": print "Matched exception<br />";
4
10477
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 file contains 'locked' information def open_file(file_name):
3
423
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 directory is not there). Is there a way to do this? Am I approaching this all wrong?
13
2334
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 Application.Run(new Form1()); i have setup a global keyboard hook
6
3404
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 catching the exception in Win32Release mode.
1
177
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 a single point. If some type of exception occurs the execution flow should continue and if other type of excetion occurs it should redirect to an error page. I have tried this by hadling exceptions in
0
2990
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 exception, but the program continues (like +e in bash). Reason: While a programmer can do his best and wrap almost every function into a try... except... statement, there is always the possibility that one forgets such a wrapping. Also, wrapping...
3
2287
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 SqlException ....display error
0
10171
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11191
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10703
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8268
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7443
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4550
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.