how to resume next in a try catch ?

Posted on 2007-12-03
Last Modified: 2011-04-14
I need in a try catch statment trap error 57 and then resume next - now in vb6 i used to use on error goto
how do u do this now in .net?
i am copying a file from one folder to another and get an error if the file is already in the destination folder


Open in new window

Question by:CraigLazar
  • 2
  • 2
  • 2
  • +2

Expert Comment

ID: 20401529

You can put try...catch around the part that might produce an error. Below that use "finally" to invoke code that always needs to be processed whether an error occurs or not

... your code
catch ex as exception
... handle exception
... do things after try block or after error
end try

hope this helps

Author Comment

ID: 20402253

is there no way i can test the err.number that is returned and then handle it that way?
just so used vb6 - and how do u control the process flow, so if an error occurs hw do u handle errors that u know and processing around apposed to errors that u are not expecting?
LVL 18

Expert Comment

ID: 20404054
Where does 'Error 57' come from?  If it's an old vb error number you should know the .Net doesn't use the old error numbers and won't throw the same error.  

        'change false to true to just overwrite an existing file
        IO.File.Copy(strSourceFileName, strDestFileName, False)
    Catch ex As IO.IOException
        'This is the exception thrown if the destination file already exists,
        'though there may be other things that cause an IOException as well
    Catch ex As Exception
        'This will catch any other errors with the copy
    End Try

Of course, it's best if you don't allow an exception to be thrown at all:

    If Not IO.File.Exists(strDestFileName) Then
        IO.File.Copy(strSourceFileName, strDestFileName, False)
        'File already exists
    End If

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20404185

*** But *** you cannot MIX Try/Catch with "On Error" in the SAME Sub/Function.

You can certainly mix the two in the same application...just not in the same sub/function.

Expert Comment

ID: 20404339

as jcoehoorn states, it is ALWAYS preferable to avoid an exception if possible.
For example, if you know the file *might* be in the destination folder already, then check first (using the file.exists function as mentioned) and if it does then take the appropriate course of action (overwrite it, inform the user, etc...).
In your scenario, I believe the function actually has a parameter that specifies whether to overwrite a file if it already exists.

Also, I would strongly recommend NOT using the old style "on error blah blah blah" syntax as it leads to so-called spaghetti code which is difficult to debug.

Best Regards,


Author Comment

ID: 20455302
hi guys thanks for the input,
jcoehoorn if the error is caught As IO.IOException, then how do i then return to the next line of execution?
what i mean is i catch the error write some code to handel it, then how do i send the execution to the line after the code that generated the original error? very similar to the resume next statement in the old On error goto statement?


LVL 18

Accepted Solution

jcoehoorn earned 50 total points
ID: 20457080
You can use "On Error Resume Next" if you really want to.  It's just poor practice and you can't mix a try/catch and the old style in the same method.  

Otherwise use smaller try blocks.  You should know about where the error you're expecting will be thrown, and can place your try and catch statements to only cover the code required.    Try blocks are designed to handle specific errors, not as a substitute for not knowing what your code will do in a large block.  Why don't you show us the code in question and then we can use that code to show you how to use exceptions correctly.
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20457549
When using Try...Catch, you can create a boolean flag (toggle it when an exception occurs) that you can check in the Finally block or after the Try...Catch structure.  If you need to repeat a piece of code while trapping errors then you enclose the entire Try...Catch in a looping structure...making sure to allow it to drop out when the proper conditions are met.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question