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?
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?
I think what you're looking for is Finally. For example:
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: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
Contributions:
Login | Let the user chose a connection string | Open/Close a Database Connection | Stick Notes | Slideable Controls | Circular Progressbar | Rounded Button | Random between 2 solutions | Custom Monthcalendar | Country Flags | XNA Control in Win-Form App | Textfile to Datatable | Syntax Highlighter
Game Contributions:
Slots | Tic-Tac-Toe | Pong | Bouncy Ball | Simon | Tron - Lightcycles | Connect4 | Hangman | Pure VB 2d Tile Based Map Engine | Plinko
XNA in Vb.Net Tutorials:
Getting Started | Setting up XNA Graphics | Displaying XNA Graphics
Links:
LegalShield | AUP
You can add Finally Statement in your Try and catch block as Dday said.
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=DDay: The finally will execute wether or not the exception is thrown.
@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
So essentially then I would want my code to be:
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:Code:Try: Run this code Catch: 'No code End Try
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.
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:
Who knows someone on here may know a better way to do it than this!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
Last edited by Jo15765; Sep 29th, 2012 at 01:39 PM.
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!"
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!
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
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
Perfect, that got it up and going! Thanks to all for the assistance!