How do I trap an "Exception" in VB.net?

My program is crashing and I don't know why.
When I run it from VS (2022 64bit), it doesn't give me an error. It simply ends. The output windows reports:
"Exception thrown: 'System.ArgumentException' in Microsoft.VisualBasic.dll"
Doesn't even give me a line number. I have no idea what is causing my program to crash. How do I debug this?
TIA
VB
-
Anonymous
2023-07-30T21:49:48+00:00 Hi
First off. Is the Error List tab at bottom of editor window - this should show errors with a double click to go to the error line. If you do not see the Error List tab then go to the View Menu and select it there.
Exceptions can best be dealt with using a 'Try .... Catch ..... Finally ..... End Try' block, where you can trap any/all/particular exceptions and deal with them cleanly. The old On Error, though still supported is really not up to the task in this environment and is best ignored/not used.
Debugging can be aided by using Break Points and single step in code. This is where you can set a Breakpoint on a line of code and execution will stop there allowing a view of variable values etc and/or single step through next lines and/or Continue execution. The easiest way to toggle a Breakpoint is to double click on the left most window border at the line where you want the Break Point to be set/unset.
There is a LOT LOT LOT LOT more to this topic, so just keep asking questions - lots of helpful folks here will help.
-
Mugsy's RapSheet • 176 Reputation points
2023-07-31T00:06:20.3266667+00:00 Thx for the reply.
Yes, the "Output" window below the code is displaying the error, which I copied & pasted above. No line number is given.
Since I do not know where the error is taking place, I haven't the slightest clue where to place a "Try/Catch" block (it probably wouldn't work anyway in this instance since the program spontaneously quits when the error occurs.
-
Jiachen Li-MSFT • 33,451 Reputation points • Microsoft Vendor
2023-07-31T06:03:00.8833333+00:00 Hi @Mugsy's RapSheet ,
Switch to the Error List bar at the bottom, where you should see the error, double-click to go to the error row.
-
Anonymous
2023-07-31T09:27:49.0633333+00:00 Hi
Maybe if you actually read my comment, you will see that the ERROR LIST is the one that LISTS THE ERRORS.
-
Mugsy's RapSheet • 176 Reputation points
2023-07-31T11:39:49.79+00:00 Thx for the reply, but the "Error List" is empty reporting "0 errors".
-
Mugsy's RapSheet • 176 Reputation points
2023-07-31T11:44:00.84+00:00 (Dupe)
-
Anonymous
2023-07-31T12:03:17.81+00:00 Hi
What happens when you select 'Rebuild Solution' from the Build menu? (Status bar text)
And, try single stepping (use F5) and follow highlighted line(s) - see what happens.
-
Mugsy's RapSheet • 176 Reputation points
2023-07-31T12:54:04.99+00:00 No change after rebuild. Program still spontaneously ends, reporting "Exception thrown:" in the Output window. No line number. "Error List" tab still empty.
Single-stepping using F8 helped me locate the offending line: renaming a folder using the original file name... including extension... is apparently a problem.
Bizarre that such an insignificant issue would cause an error so severe it doesn't even report a line number or error message where the error takes place. Just a dead-stop crash.
Thx.
-
RLWA32 • 47,206 Reputation points
2023-07-31T12:54:47.6633333+00:00 I noticed that the common language runtime (CLR) version in the error message was 2.0.50727. This indicates that thecode is targeting a very old version of the .Net Framework. Have you tried targeting .Net Framework 4.8?
-
Mugsy's RapSheet • 176 Reputation points
2023-07-31T12:56:07.87+00:00 Unfortunately, I must use the minimum compatible Framework for maximum compatibility across Windows versions.
-
RLWA32 • 47,206 Reputation points
2023-07-31T13:04:32.22+00:00 Unfortunately, I must use the minimum compatible Framework for maximum compatibility across Windows versions.
As a practical matter, version 3.5 of the .Net Framework is an optional Windows component and may not be installed on many systems. So this argument isn't as persuasive as you may believe. You may find this information to be helpful - .NET Framework versions and dependencies
-
Mugsy's RapSheet • 176 Reputation points
2023-07-31T13:12:14.2533333+00:00 Originally, the app supported XP (which topped out at DotNet 3.5.1.)
I can bump that up to Win7 now, which I believe the last version it supported was 4.5.1.
-
RLWA32 • 47,206 Reputation points
2023-07-31T13:15:13.5633333+00:00 .Net Framework 4.8 is supported on Win 7.
-
Mugsy's RapSheet • 176 Reputation points
2023-07-31T13:49:29.6533333+00:00 Ugh! After changing the "Target Framework" to "DotNet 4.5.2", my "frmMain.Designer" now contains a flood of "errors" and the form itself no longer appears for editing in Design Mode.
How to I correct "design mode" errors created by VS itself?
(Tried "Rebuild.")
-
Viorel • 119.9K Reputation points
2023-07-31T19:32:34.24+00:00 Regarding the initial problem, did you try to go to menu, DEBUG, Windows, Exception Settings, then find and select the System.ArgumentException? Then start your program in debug mode and check if it gives some details. However, it is better to consider a newer acceptable version of .NET.
-
Mugsy's RapSheet • 176 Reputation points
2023-08-01T00:02:33.95+00:00 Thx for the reply.
Unfortunately, the suggestion that I update the Target Framework from DotNet 3.5 to 4.6 somehow produced errors throughout my startup form.
Now it's chock full of errors preventing me from even displaying in the the GUI editor.
I can't test your suggestion until I figure out how to get my project working again. :(
-
Jiachen Li-MSFT • 33,451 Reputation points • Microsoft Vendor
2023-08-01T09:25:21.6333333+00:00 Hi @Mugsy's RapSheet ,
Try creating a new .NET Framework 4.5.2 project, compare the differences in designer.vb file formats between the two, and then modify where the error is reported.
Sign in to comment