Solved

how to resume next in a try catch ?

Posted on 2007-12-03
8
2,913 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 86

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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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