Re: Try...Catch... Resume??



I was hoping to avoid using many Try ... Catch constructs repeatedly. What
I'm trying to accomplish is basically ignore an element error when reading
thru an XML file -- if the element isn't in the file just continue reading
and ignore -- this allows me to work with XML structures that change where
I'm only interested in certain parts of the XML file.

MSDN states that I can't use a Resume statement in a Try...Catch -- either
use one approach or the other. I like the Try..Catch construct, but I was
hoping that I wouldn't loose my Resume functionality. .NET still supports
the On Error ... Resume so I guess I'll have to use that for this instances.

thanks, Rob.


"Michael" <Michael@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:543AFD2C-3B91-42C9-B750-53A6C11F6DCE@xxxxxxxxxxxxxxxx

Rob,

I think that if you change the scope of the try...catch you will find that
you can accomplish what you are looking for. Take the following pseudo
code
for example...

Try
For x = 1 to 100
' Do something that causes an error occasionally.
Next
Catch Exception


Using the preceding exception handling you cannot return to the loop to
continue. But if you simply change it to:

For x = 1 to 100
Try
' Do something that causes an error occasionally.
Catch Exception
Next

You're code can continue to work on the loop. When you catch exceptions
you
want to keep it to the smallest scope possible and want to catch the most
specific exception possible. By this I mean, assume that you are using
File.Copy to copy a file. Looking in the MSDN documentation you can see
that
it can throw 8 different exception types. You may be tempted to catch
System.Exception just to have one catch block but with a little
forethought
you do not need to worry about all 8 of them. You can check the arguments
that you pass in before to make sure that they are valid and eliminate the
possibility of it throwing ArgumentException, ArgumentNullException,
DirectoryNotFoundException, FileNotFoundException, and NotSupportedOption.
If you have checked for permissions or used code access security to
specify
your permissions you can avoid the UnuathorizedAccessException. Thus, you
would just need to catch the IOException which you can not really plan
around
since you cannot know ahead of time if your hard drive is going to crash.

I hope this long winded answer is close to what you were looking for.

Michael
"Rob R. Ainscough" wrote:

In VB6 I can use Resume Next to execute the line of coding following the
line that cause an exception. There doesn't appear to be anything
similiar
when using Try...Catch -- so how can one resume the next statement when
an
error can be ignored?

Thanks, Rob.





.



Relevant Pages

  • use of unassigned local variable
    ... I am writing code to read an xml file. ... so I want to save them as I am reading through the elements. ... catch (Exception e) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: default namespace / Xmlvalidatingreader
    ... You dont get an exception in the case of the second file, ... validation warning should be thrown. ... XmlReader readerToUse = validatingReader; ...
    (microsoft.public.dotnet.xml)
  • Re: XmlValidatingReader / Default Namespace
    ... You dont get an exception in the case of the second file, ... validation warning should be thrown. ... XmlReader readerToUse = validatingReader; ...
    (microsoft.public.dotnet.xml)
  • Re: Ole Automation of Project 2000
    ... > Project and read/save data to Excel for analysis. ... Reading a blank line causes my application to ... > an exception but I have not been able to track down the source of the ... I have experimented with the windows function VarIsEmpty, ...
    (microsoft.public.project.vba)
  • Re: Xml validation exception only occurs outside debugger - VS 200
    ... I'm able to reproduce the problem on any Xml file (even ones produced by ... Could you attach the entire stack trace and message for the exception? ... outside the debugger I receive the following error: ...
    (microsoft.public.dotnet.xml)