Solved

how to resume next in a try catch ?

Posted on 2007-12-03
8
2,906 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
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
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

16 Experts available now in Live!

Get 1:1 Help Now