tags:

views:

142

answers:

7

When we get an objects that is actually are exceptions, we can do with them anything that we can do with ordinar objects in our language. We can pass them as an argument, we can store them in some collection and, what is the worst, we can return them as a result from the methods!

So there is a possibility for someone to write smelly code like this:

public Exception doSomethingCritical()
{
    Exception error = null;

    try
    {
        ...
    }
    catch (Exception e)
    {
        // maybe at least here is the logging of the error if we are lucky
        error = e;
    }
    return error;
}

So the question is why is the concept of an Exception-object is a first-class citizen in many OO languages? Maybe it is better if we have only limited constructions in the language that is allowed for exception-objects (like throw).

A: 

Because life is a lot easier that way. Among other things, it means that the exception object can contain information that the program can use to correct the exceptional situation (perhaps with human intervention).

Abend is so 1960s.

kdgregory
A: 

I'm not sure the example you give is a strong enough case for not having exceptions as objects. After all you COULD do all kinds of "smelly" or "bad" things while programming. However thats precisely the reason we want good programmers. Just because I can do dsomething like :

def get_total
   return nil
end

surely doesnt mean I should not allow nil as an instance of an object!

ennuikiller
why is so "surely"? ;-) maybe some language can disallow nil for any objects, but allow it for some special cases and it will be good.
alsor.net
+1  A: 

How would you be able to inherit from the base exception class and derive your own exception class for your module if they were not first type citizens?

Chad
+6  A: 

The problem with treating exceptions as a special case, and allowing only limited functionality with exceptions is that ultimately somewhere every good program needs to do something with exceptions, whether it be a global error-handler or a limited item that appropriately deals with the exception. By making exceptions first class citizens, there are many benefits, such as:

  • Allowing subclassing of exceptions. This is invaluable for exception handling as code dealing with exceptions can narrow their scope and deal only with exceptions they know how to deal with. I'd go so far as to say that any non-global exception handling routine that is catching just "Exception" is probably doing it wrong.
  • Passing data along with exceptions. Exceptions aren't very useful if the only thing you know in your catch logic is that an exception occurred. Stack traces, messages and even custom data for certain exception types are invaluable in identifying and resolving the problem that caused the exception.
  • Creating error handling routines that use OOP themselves. If exceptions couldn't be passed as objects, you couldn't for instance have a library that deals with exceptions - well, at least not a well written one.

Besides all of that, there's no way to guard against bad exception handling like you posted above. Even if an exception wasn't a first class citizen, there's no way to stop a developer from eating any and all exceptions and carrying on their merry way (at least, without fundamentally breaking how we think of exceptions)

Ryan Brunner
Access to the class structure is a big, important piece of this. Catch IOException some time to see how useful it is for catching a whole category of errors.Yes, you could do it some other way, but why reinvent the wheel?
Alex Feinman
+2  A: 

I've never actually seen that example you've used. And I can't see how not allowing people to return exceptions will make a big difference to conceptually poor code - compare

public int doSomethingCritical()
{
    int error = 0;

    try
    {
        ...
    }
    catch (Exception e)
    {
        // maybe at least here is the logging of the error if we are lucky
        error = E_SOMETHINGBAD;
    }
    return error;
}

Whereas if you create a new kind of "thing" to be used for exceptions that isn't the same as an object, there is a design and learning overhead disadvantage.

pjc50
Very good point. And I think in a language that disallows exceptions as a return values my example looked exactly like this one.
alsor.net
+1  A: 

I don't see why I shouldn't be allowed to pass and return exceptions as normal method parameters. What if I were writing an Exception Handling library? What if I were writing a unit test assertion that compared exceptions?

Christian Hayter
+1  A: 

My opinion.

I think you are confusing two different things: the Exception and the Exception throwing.

The exception throwing is an out-of-band process that allows you to throw an object through a preferential, lateral channel. This object can be intercepted and handled through the Exception catching mechanism.

The Exception is just one object that you can (preferentially) throw via the Exception throwing out-of-band channel. Of course, this channel can have requisites for the interface of the object being thrown, requisites that are satisfied by the Exception class interface.

You are looking at the issue the other way around. Indeed you can do horrors, but there's nothing special about the Exception object, apart of being the preferred object that is thrown in the out-of-band channel.

Stefano Borini
Really good point of view. I wish all programmers understand exceptions in a such way. But can we do something in the language for that?
alsor.net
Well, technically, you could, but then you would have to invent additional semantics for the exception object to be not a class, but something else which is not a class. Nobody wants that, because you make the language core more complex and less flexible. Basically, a loose-loose situation.
Stefano Borini
And it would go against the (good) trend of considering "everything as an object". In python for example, this is so pervasive that classes, modules, exceptions, functions (named and anonymous), datatypes are all objects.
Stefano Borini