Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2952
  • Last Modified:

how to resume next in a try catch ?

Hi,
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

thanks

Open in new window

0
CraigLazar
Asked:
CraigLazar
  • 2
  • 2
  • 2
  • +2
1 Solution
 
john_steedCommented:
Hey,

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

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

hope this helps
0
 
CraigLazarAuthor Commented:
Hi,

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?
0
 
Joel CoehoornDirector of Information TechnologyCommented:
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.  

    Try
        '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)
    Else
        'File already exists
    End If

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
YOU CAN STILL USE THE OLD "ON ERROR" STYLE HANDLING IN VB.NET.

*** 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.
0
 
doobdaveCommented:
Hello,

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 system.io.file.Copy() 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,

David
0
 
CraigLazarAuthor Commented:
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?

thanks

0
 
Joel CoehoornDirector of Information TechnologyCommented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now