Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception signature

What's the signature for class Exception in python 2.x?

I wish to subclass it and add arguments of my own, while also correctly invoking super.

The following code works:

class FooError(Exception):
    def __init__(self, msg, x):
        super(FooError, self).__init__(msg)
        self.x = x

But, is there some documentation or reference? pydoc Exception is not helpful. Neither is the documentation: this or this.

like image 836
user650654 Avatar asked Aug 09 '13 22:08

user650654


People also ask

What is signature exception?

SignatureException(String message, Throwable cause) Creates a SignatureException with the specified detail message and cause. SignatureException(Throwable cause) Creates a SignatureException with the specified cause and a detail message of (cause==null ? null : cause.

How do I add an exception to a signature?

To use a method that declares an exception in its signature, you MUST either: provide exception handling codes in a " try-catch " or " try-catch-finally " construct, or. not handling the exception in the current method, but declare the exception to be thrown up the call stack for the next higher-level method to handle.

Is exception part of method signature?

Method Signature According to Oracle, the method signature is comprised of the name and parameter types. Therefore, all the other elements of the method's declaration, such as modifiers, return type, parameter names, exception list, and body are not part of the signature.

What do you mean by exception handling?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.


1 Answers

This part of the Python documentation is not very explicit regarding method signatures, but there are some hints.

Quoting the Python Docs (6. Built-in Exceptions):

...Except where mentioned, they have an “associated value” indicating the detailed cause of the error. This may be a string or a tuple containing several items of information...

BaseException states the following:

args

The tuple of arguments given to the exception constructor.

And Exception:

Changed in version 2.5: Changed to inherit from BaseException.

The rest of the information can only be found in the sources (I think)

static int
BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) {
    if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
        return -1;

    Py_DECREF(self->args);
    self->args = args;
    Py_INCREF(self->args);

    if (PyTuple_GET_SIZE(self->args) == 1) {
        Py_CLEAR(self->message);
        self->message = PyTuple_GET_ITEM(self->args, 0);
        Py_INCREF(self->message);
    }
    return 0;
}

As you can see it allows no keyword arguments. args is initialized as a tuple in new.

So all in all the signature would be:

def __init__ ( self, *args )

And inheriting from Exception (without restricting the arguments)

class FooError ( Exception ):
    def __init__ ( self, x, *args ):
         Exception.__init__ ( self, *args )
         self.x = x

I always call the base class explicitly, I don't like super. But this is just a question of style.

Note that your code is correct. You just restrict Exception to receive only one argument, while it could handle a tuple.

like image 118
mkiever Avatar answered Sep 22 '22 02:09

mkiever