Results 1 to 15 of 15

Thread: [RESOLVED] Try Catch - Resume Next?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Resolved [RESOLVED] Try Catch - Resume Next?

    I have a case where I need to resume execution on the next line after an exception. Is there no way to do this using the structured error handling? I realize that I can go back to the "On Error .. Goto", but I can't believe there's not a way to do this with structured error handling...

  2. #2
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Try Catch - Resume Next?

    The main point of Try...Catch blocks is to catch an error then exit the function.
    You can use Finally to execute remaining code whether or not an error occurred.

    Like

    vb.net Code:
    1. Try
    2. ' Some code
    3. Catch aex As ArgumentException
    4. ' Some exception code
    5. Finally
    6. ' Can be used to dispose of objects created in this method.
    Prefix has no suffix, but suffix has a prefix.

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Try Catch - Resume Next?

    You could try and place a Try Catch around the statement with nothing in the Catch area:

    Try
    Statement Here
    Catch ex as Execption
    End Try
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    New Member
    Join Date
    Apr 2007
    Posts
    8

    Re: Try Catch - Resume Next?

    Well, why not use Exit Sub or Exit Try which will also keep you moving along.

  5. #5
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: Try Catch - Resume Next?

    You could try and place a Try Catch around the statement with nothing in the Catch area:
    If you are not going to catch the exception then why use the Try...Catch statement?

    Maybe a snippet of code, tim8w?
    Prefix has no suffix, but suffix has a prefix.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,495

    Re: Try Catch - Resume Next?

    I'm just continuing after the execption similare to the old VB6 On Error Resume Next
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Red face Resolved - Sort of: Try Catch - Resume Next?

    I'm beginning to see that I have little choice than to for this one function, to use the old error handling method. On Error Goto - Resume Next. Thank goodness they left this option available or I would have had to have 180 Try...Catch's in this function...

    I do care that the user is aware of the error, but I want the program to continue processing the remaining data...

  8. #8
    New Member
    Join Date
    Apr 2007
    Posts
    8

    Re: [RESOLVED] Try Catch - Resume Next?

    Well, yeah?? This way, you will see the error box in the program then will keep moving along to the next line in the code like so:

    Try

    ' Trap Error

    Catch EX as Excetion

    MessageBox.Show(ex.message)

    Exit Try ' Exit catch and read next line!

    End Try

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] Try Catch - Resume Next?

    There's no reason for you to use On Error - it's bad. A try/Catch block can do the same thing but much better.

    Take this rough example. The 2nd Try/Catch will resume, and keep processing even if an error is encountered. The first one will stop once an error is encountered and not process any other items. It all depends where you put the catch block.

    vb Code:
    1. Dim myNumbers As New List(Of String) 'I know this isn't right but serves as a good example
    2. myNumbers.Add("34")
    3. myNumbers.Add("4")
    4. myNumbers.Add("hi")  'This will be an error
    5. myNumbers.Add("12")
    6. myNumbers.Add("9")
    7.  
    8. Dim myTotal As Integer = 0
    9.  
    10.         Try  'This try block will then stop once it gets to Index 2
    11.  
    12.             For i As Integer = 0 To myNumbers.Count - 1
    13.                 myTotal = CInt(myNumbers(i)) * 10
    14.                 MessageBox.Show(myTotal.ToString)
    15.             Next
    16.  
    17.         Catch ex As Exception
    18.             MessageBox.Show(ex.Message)
    19.         End Try
    20.  
    21.  
    22. 'COMPARE it with this one which will keep processing the remaining items
    23.         For i As Integer = 0 To myNumbers.Count - 1
    24.  
    25.             Try  'This Try block will resume, and try the next one
    26.                 myTotal = CInt(myNumbers(i)) * 10
    27.                 MessageBox.Show(myTotal.ToString)
    28.             Catch ex As Exception
    29.                 MessageBox.Show(ex.Message)  'You can take this out if you really need to
    30.             End Try
    31.  
    32.         Next
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  10. #10
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [RESOLVED] Try Catch - Resume Next?

    Good example stimbo. tim8w never posted any code so I had no idea where he was using the Try...Catch statement.
    Prefix has no suffix, but suffix has a prefix.

  11. #11
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] Try Catch - Resume Next?

    Cheers,

    I've just sort of assumed it's the placement that causing the problem rather than the Try/Catch block per se.

    Why people don't post the code that's causing the problem is beyond me.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Red face Re: [RESOLVED] Try Catch - Resume Next?

    Stimbo,
    Thanks for the example. Unfortunately, I don't have data that I can put in a loop. If I did, your example would be perfect. As I mentioned before, the only way I can see using Try...Catch would be to bracket each and every place that could cause a error in the routine...

    Thanks for all the suggestions.

  13. #13
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] Try Catch - Resume Next?

    If you have that many places where an error could occur it's usually an indicator that the data hasn't been properly filtered or controlled for enough prior to being entered into the function.

    Still guessing though, but you know your own code better than anyone so I'll leave it at that.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] Try Catch - Resume Next?

    what exactly does your code do that a possible 180 error can occur in one routine while its running?

    You might be able to write the routine better...

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Red face Re: [RESOLVED] Try Catch - Resume Next?

    The main problem is this. The routine must be able to work for all versions of the database. Unfortunately, the database doesn't contain any information as to what version it is. The routine is importing the old Access databases into a new SQL2005 database. The 180 figure is an exaggeration. Basically for any of the values that are not in the old Access database, an error is triggered and displayed. This will help the user know that this value may need to be modified in the new database, if the default is not acceptable. Also, in the SQL2005 database, field names were modified so that they don't contain any spaces and/or dashes. The old databse was full of field names like this. If this weren't the case, I could loop through the new database and use the code you all suggested...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width