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

try catch isn't catching the error

1 REPLY 1
Reply
Message 1 of 2
ssomick
430 Views, 1 Reply

try catch isn't catching the error

in the drawing, one of my pipe network structures doesn't have a station property & caused an error when the code called for that property to be read in the first IF in the code below.

 

I'm not posting thousands of lines of code since the other aspects of the routine that the posted code segment is from have worked just fine until this particular situation. I know why the error is occuring & I know what to do in the drawing to make it not happen, but I'd like to be able to handle the error so that users of the routine will be informed.

 

so I added a TRY statement to catch that error & the error would keep occuring instead of being handled. so I though maybe TRY doesn't work on IF statements, so I added the "tempstation" variable declaration line. the error is still not getting caught.

 

the inner TRY / CATCH is not relevant to this discussion, I'm sure.

 

Then in the editor I set DEBUG > EXCEPTIONS>>> to have everything be thrown, as originally nothing was set to be thrown. That had no affect.

 

So can anyone tell me why this isn't working (they way I think it should)?

 

 			Try

                                Dim tempstation As Double = oStructure.Station

                                If oStructure.Station < StructureList(i).Station Then

                                Try

                                    StructureList.Insert((i), (oStructure))

                                    booApend = False
                                    GetAllStructuresFromAlignment = True

                                    Exit For

                                Catch

                                    GetAllStructuresFromAlignment = False

                                End Try

                            Else

                                booApend = True

                                End If

                            Catch ex As Exception

                                msgbox("structure" & ostructure.name & " has a station ERROR")

                            End Try

 

1 REPLY 1
Message 2 of 2
Jeff_M
in reply to: ssomick

You are 'catching' an Exception, but since we can't see what your Imports include my guess is you are not Importing System so the Exception is an AutoCAD.Runtime.Exception. The Station property throws a CivilException which is caught if it is specifically referenced OR a System.Exception will also catch it. It's a good rule of thumb to determine what kind of exception is thrown under certain circumstances and catch that specific exception, then if you get thrown into another exception find what is causing that, rather than using a catch-all exception (which is essentially what catching a System.Exception would do).

 

So changing

Catch ex As Exception

 

to 

Catch ex as Autodesk.Civil.CivilException

 

should fix this immediate issue.

Jeff_M, also a frequent Swamper
EESignature

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost