Exceptions in classes

  • Thread starter Thread starter Jason MacKenzie
  • Start date Start date
J

Jason MacKenzie

I'm wondering what the best way to pass an exception up to an aspx page (for
example) from my object is.

If I have a method in my class

public function Test() as string

try

catch eEx as exception

end try

end function

In my aspx page:

dim myObj as new TestObject()

dim strString as string = myObj.Test

I guess what I'm wondering is the best way to handle an exception that may
occur in my TestObject class in my aspx page.

I hope that made sense,

Jason MacKenzie
 
Ok, catch and deal with the

exception if you can. In this

case you can return from

your method and the ASP page will never know.



If you can't resolve the problem (because it is more than the method/class
responsibilities) you could re-throw the exception and catch it at your
page.



In some circumstances you should treat as you can and throw a "custom"
exception object, to make the caller more capable to deal with it.
 
Hi Jason,

If you have
Try
DoSomething
Catch ex As Exception
Throw ex
End Try
you are adding no value to the error and shouldn't bother catching it.

But with
Try
SomeVar = "Blah" 'Or this may be passed in by the caller.
DoSomethingWith (SomeVar)
Catch ex As Exception
Throw New Exception ("SomeVar was <" & SomeVar & ">", ex)
End Try
you are adding value, ie providing additional information.

The other reasons for catching Exceptions, of course, are so that you can
actually deal with it and not have to worry the caller at all, or when you
need to do some tidy-up actions allowing you to gracefully retire.

Regards,
Fergus
 
Back
Top