Results 1 to 11 of 11

Thread: [RESOLVED] Try....Catch Help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Resolved [RESOLVED] Try....Catch Help

    I see that from VBA to VB.Net the On Error Resume Next statement has gone out the window. I was wondering if there was a way to use a Try...Catch, but let the Catch continue on with the code as if there was no error. Essentially the Resume Next?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,389

    Re: Try....Catch Help

    I think what you're looking for is Finally. For example:

    Code:
            Dim twentyTwo As Integer = 0
            Try
                If TextBox1.Text = "Terse" Then
                    twentyTwo = 22
                End If
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            Finally
                MessageBox.Show(twentyTwo.ToString)
            End Try
    What is going on in that statement is, try to see if textbox1's text is Terse, if it is, then set a variable named twentyTwo. The catch is there to make sure that all goes well. The finally will execute wether or not the exception is thrown.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Try....Catch Help

    You can add Finally Statement in your Try and catch block as Dday said.

  4. #4
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Try....Catch Help

    DDay: The finally will execute wether or not the exception is thrown.
    As will any code following the End Try statement. I don't think your reply answers the OP's question. What is different between the code in the Finally block and code that follows the Try - End Try=

    @OP, If you want to ignore the error, as On Error Resume Next did, then you simply need to put no code in the Catch part, though this is not good practice. Errors are there for a reason and need to be notified and handled.
    Last edited by Españolita; Sep 29th, 2012 at 02:34 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Try....Catch Help

    Quote Originally Posted by españolito View Post
    As will any code following the End Try statement. I don't think your reply answers the OP's question. What is different between the code in the Finally block and code that follows the Try - End Try=

    @OP, If you want to ignore the error, as On Error Resume Next did, then you simply need to put no code in the Catch part, though this is not good practice. Errors are there for a reason and need to be notified and handled.
    So essentially then I would want my code to be:
    Code:
    Try:
    Run this code
    Catch:
    'No code
    End Try
    Is that correct? I want to add this in because I am using VB.NET to open Excel and refresh queries in the workbook, but I KEEP getting this error:
    System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

    but if I just try to open Excel and Refresh normally all queries refresh fine. Does anyone know what would be causing that error?
    Last edited by Jo15765; Sep 29th, 2012 at 12:54 PM.

  6. #6
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Try....Catch Help

    this might be useful reading.
    Apparently you can set the IDE options to notify or ignore all first chance exceptions.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Try....Catch Help

    I read the article you provided, thank you for that, and found http://www.helixoft.com/blog/archives/24 article on how to disable 1st chance exceptions. Now instead of my code breaking and going into debug mode, it gives me the option to break or continue. Is there an option, to just completely ignore and continue processesing? If it helps this is the code I am trying to execute:
    Code:
        Public Shared Sub QueryRefresh()
            Dim QT As Excel.QueryTable
            For Each sheet In xlsApp.Worksheets
                For Each QT In xlsSheet.QueryTables
                    QT.Refresh(BackgroundQuery:=False)
                Next QT
            Next sheet
        End Sub
    Who knows someone on here may know a better way to do it than this!
    Last edited by Jo15765; Sep 29th, 2012 at 01:39 PM.

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Try....Catch Help

    The reason they took out the Resume Next option is because it's a really dumb thing to do. You're simply delaying the inevitable and making it more difficult to debug. But if you insist ...

    vb.net Code:
    1. Try
    2.             'line that might throw an exception
    3.         Catch 'nothing happens and we just carry on
    4.         End Try
    5.  
    6. 'next line
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Try....Catch Help

    I'm afraid I'm out of my depth now so I'll stand back and let someone more experienced step in. However, I would say three things.
    First, I would expect an empty catch to override the IDE and so remove the error.
    Second, and most importantly, I still stand by my previous comment about ALL errors needing to be caught, notified and handled. I'm surprised that the IDE has a two pass system and allows first passes to go.
    Thirdly, This seems to be an IDE thing. Would the published .exe (if it is different to the .exe generated when run in debug mode) behave the same?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Try....Catch Help

    A try with an empty catch is going to catch the exception, which will be about the slowest thing you can do. Normally, I would suggest that you never do this, because it is normally a horrible thing to do. However, there is one case where I do this (where a meaningless exception is occasionally raised during some kinds of Socket messaging). You may have found a second case. Typically, I would stufy the problem to try to figure out what is causing the exception, but COM exceptions can be so maddeningly undocumented that it is one area that you simply may not be able to figure it out. That would be a pain in the butt, as you would be accepting poor performance as a tradeoff for opaque misunderstanding, which is not a bargain most people would be happy with, but COM really does suck at times.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: Try....Catch Help

    Perfect, that got it up and going! Thanks to all for the assistance!

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