.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Try / Catch statements are not working

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
1359 Views, 2 Replies

Try / Catch statements are not working

I have been trying to use Try statements but they do not seem to be working correctly.

 

<CommandMethod("TestTryCatch")>
Public Shared Sub sbTestExceptions()
'************** Try and catch statements do not work
MsgBox("Start")
Try
'Throw New SystemException()
Dim oReader As System.IO.StreamReader = New StreamReader("c:\NoFile.txt")
MsgBox("Should not get here.")
oReader.Close()
Catch ex As Exception
MsgBox("This should show but doesn't")
End Try
MsgBox("This should not show and doesn't.")
End Sub

 

Program execution does not go to the catch statement, instead I get  "Unhandled exception has occurred in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.

Could not file file 'c:\NoFile.txt'."

When I click continue I am back to where I was before running the command.

 

I first noticed this problem when I put a throw statement in a called routine hoping I could catch it in the top level routine. 

 

Any ideas as to why this is happening would be appreciated.

 

2 REPLIES 2
Message 2 of 3
norman.yuan
in reply to: Anonymous

I do not regularly use VB.NET (try hard to avoid it whenever it is possible!), but I guess it could only happens when you use VB.NET. 

 

Since you also show all the "Imports ...." on top of your VB.NET code file, or the imports in the project properties (another thing I really do not like for VB.NET: it trys to "help" user too much, which make the same tast - Imports... - in multiple locations!), is is hard to tell in "Catch ex As Exception" line which class the "ex" is: is it System.Exception, or Autodesk.AutoCAD.Runtime.Exception?

 

If both "Imports System" and "Imports Autodesk.AutoCAD.RunTime" applies the code file, you would not be able to compile the code with "Ambiguity" error, because the compiler could not tell "ex As Exception" is System.Exception, or Autodesk.AutoCAD.RunTime.Exception. At least, if it is C# code, the building process would prompt you.

 

In your case, I think, the ex As Exception is actually being Autodesk.AutoCAD.RunTime.Exception, thus, the Catch clause would not be able to catch it, because the exception your code raised is an System.SystemException.

 

If you do not want to only catch specific exception, you should change the code to

Try

...

Catch ex As System.Exception

    '' All exception will be caught, because all other Exceptions are derived from System.Exception

    '' (well, there could be odd cases in AutoCAD that AutoCAD may still crash with no exception being caught,

    '' but that is extremely rare and is not your case at all).

End Try

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
Anonymous
in reply to: norman.yuan

Thank you norman.yuan.

 

I changed it to "catch ex as SystemException"  and the exception was handled.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta