Results 1 to 11 of 11

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

  1. #1
    Addicted Member
    Join Date
    Nov 10
    Posts
    187

    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
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    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.

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

    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 07
    Location
    Albacete, espaņa
    Posts
    579

    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.
    A fun card game written in VBA within Excel Tri Peaks

  5. #5
    Addicted Member
    Join Date
    Nov 10
    Posts
    187

    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 07
    Location
    Albacete, espaņa
    Posts
    579

    Re: Try....Catch Help

    this might be useful reading.
    Apparently you can set the IDE options to notify or ignore all first chance exceptions.
    A fun card game written in VBA within Excel Tri Peaks

  7. #7
    Addicted Member
    Join Date
    Nov 10
    Posts
    187

    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 12
    Posts
    5,546

    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!"

    Praise for Dunfiddlin's work: What starts out as triumph soon becomes finessed into a tragedy of power, leaving only a sense of nihilism and the inevitability of a new reality.

    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 07
    Location
    Albacete, espaņa
    Posts
    579

    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?
    A fun card game written in VBA within Excel Tri Peaks

  10. #10
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,410

    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
    Addicted Member
    Join Date
    Nov 10
    Posts
    187

    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
  •