New Member
Posts: 2
Registered: ‎11-30-2012
Message 1 of 2 (153 Views)

try catch isn't catching the error

153 Views, 1 Replies
12-28-2012 07:27 AM

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)?



                                Dim tempstation As Double = oStructure.Station

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


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

                                    booApend = False
                                    GetAllStructuresFromAlignment = True

                                    Exit For


                                    GetAllStructuresFromAlignment = False

                                End Try


                                booApend = True

                                End If

                            Catch ex As Exception

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

                            End Try


*Expert Elite*
Posts: 4,519
Registered: ‎07-22-2003
Message 2 of 2 (132 Views)

Re: try catch isn't catching the error

12-28-2012 02:48 PM 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



Catch ex as Autodesk.Civil.CivilException


should fix this immediate issue.

Jeff_M, also a frequent Swamper
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.