|
-
Sep 28th, 2012, 07:09 PM
#1
Thread Starter
Hyperactive Member
[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?
-
Sep 28th, 2012, 08:02 PM
#2
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.
-
Sep 29th, 2012, 02:05 AM
#3
Hyperactive Member
Re: Try....Catch Help
You can add Finally Statement in your Try and catch block as Dday said.
-
Sep 29th, 2012, 02:27 AM
#4
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.
-
Sep 29th, 2012, 12:45 PM
#5
Thread Starter
Hyperactive Member
Re: Try....Catch Help
 Originally Posted by españolito
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.
-
Sep 29th, 2012, 01:20 PM
#6
Re: Try....Catch Help
this might be useful reading.
Apparently you can set the IDE options to notify or ignore all first chance exceptions.
-
Sep 29th, 2012, 01:36 PM
#7
Thread Starter
Hyperactive Member
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.
-
Sep 29th, 2012, 02:33 PM
#8
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:
Try
'line that might throw an exception
Catch 'nothing happens and we just carry on
End Try
'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!
-
Sep 29th, 2012, 02:34 PM
#9
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?
-
Sep 29th, 2012, 02:51 PM
#10
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
 
-
Sep 29th, 2012, 06:50 PM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|