Exception handling in base(abstract) or derived class.

H

hillcountry74

Hi,

I'm a newbie to OOP and VB.Net. In the business layer, I have a base
class, which is an abstract class in VB.Net. The derived class
overrides a method and calls other methods in the base class. To make
it more clear:

Class NotInheritable BaseAbstract
Sub Method1()
code
end Sub

Sub Method2()
code
Calls Method1
code
end Sub

Sub Method3()
code
Calls Method2
code
End Sub

Overidable Sub Method4()
End Sub

End Class

Class Derived : Inherits BaseAbstract
Overrides Sub Method4()
code
Calls Method3
code
End Sub
End Class

The UI creates an object of Derived class.

I wanted to know what's the best way to handle exceptions? I know I
should have thought about in the design phase :(

Thanks for your help.
hillcountry74
 
J

Jonathan Allen

I wanted to know what's the best way to handle exceptions?

What exception? Generally speaking, you ignore exceptions unless you know
how to handle them. Of course, a global exception error to log those you
didn't expect is a good thing.
 
H

hillcountry74

Thanks for replying.

Sorry, I was not clear in my question. What I meant was, where should I
handle unexpected exceptions- in the base or derived class?

If I handle them in the base class, how do I notify the derived class
method that an error occurred and not continue processing? The method
calls are nested so, how can I bubble up the info that an error
occurred all the way to the derived class method?
 
N

Nano

Personally I use functions in stead of methods in these cases. The
function can return True if the code executed nicely, otherwise it
returns False.

Hope this helps.

Nano
 
J

Jonathan Allen

Sorry, I was not clear in my question. What I meant was, where should I
handle unexpected exceptions- in the base or derived class?

Since you are calling it an unexpected exception, I am assuming that the
base class doesn't really know what to do with the error. That's fine.
The method
calls are nested so, how can I bubble up the info that an error
occurred all the way to the derived class method?

Simply don't catch the exception. It will keep bubbling up until it reaches
a point where you know what to do or it hits the global exception handler..


Function A
SomethingDangerous() 'this might throw an exception
End Function

Function B
Try
A()
Catch err as FileNotFoundException
Msgbox "Couldn't find the file"
Catch err as FileLockedException 'I made up this exception, don't feel
like looking up the real one
Msgbox "Another program is using that file."
End Try 'any other exception like Out Of memory will continue to
bubble up
End Function
 
J

Jonathan Allen

That has caused a lot of problems for me. We have a lot of code that does
that. The problem is, we never know why the function failed. Eating the
exception and returning false makes fixing the underlying problem very
difficult.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top