Solved

how to resume next in a try catch ?

Posted on 2007-12-03
8
2,901 Views
Last Modified: 2011-04-14
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
Comment
Question by:CraigLazar
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 3

Expert Comment

by:john_steed
ID: 20401529
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
 
LVL 4

Author Comment

by:CraigLazar
ID: 20402253
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
 
LVL 18

Expert Comment

by:jcoehoorn
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.  

    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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20404185
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:doobdave
ID: 20404339
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
 
LVL 4

Author Comment

by:CraigLazar
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?

thanks

0
 
LVL 18

Accepted Solution

by:
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.
0
 
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now