Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch all exceptions from an imported module

I have got a program (a gui with kivy) which imports a non-standard module (made by one of my co-workers) which raises exceptions (Exception) for very particular issues. I wish to catch all raised Exception from that particular module so that I can raise a popup in my program. So what's the best way to catch raised exceptions from a particular module?

I tried this

toc is the module name

import toc
...
...
try:
    ...
    ...
except toc.Exception:
    ...

But this doesn't seem to work, it produces an AtrributeError saying that the module has no attribute 'Exception'. What am I doing wrong?

Edit: One more question:

If I have:

try:
    ...
    try:
        ....
    except:
        some small block code
except:
    some large block code

will the exception handling work within the local blocks? I mean if I get an error in the smaller block, will the error handling proceed to the the 'some small block code' or will it go to 'some large block code'?

like image 617
Crust3 Avatar asked Jan 08 '13 01:01

Crust3


People also ask

How do you catch all exceptions in Python?

Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword. It is a manual process wherein you can optionally pass values to the exception to clarify the reason why it was raised. if x <= 0: raise ValueError(“It is not a positive number!”)

Can you catch all exceptions Java?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block. An old, prior to Java 7 approach to handle multiple exceptions.

Which exception catch all exceptions in Python?

Try and Except Statement – Catching all Exceptions Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.

Is it essential to catch all types of exceptions?

It's not absolutely required to have a try/catch block for your exceptions. Instead, you can throw them to someone who is able to handle the exception properly. There are 2 kinds of exceptions: Checked and Unchecked.


2 Answers

You can catch all exceptions of specific type, but you need to give correct type. Your toc.Exception is not an exception class.

Generally you have three approaches, depending mainly on the module's design:

  • if the module contains some common exception class (like it deals with some API and all API errors inherit from the base module class), catch every error using this class,
  • if the module does not have such base error class, but has own exception classes, you can catch them all,
  • if you want to catch every error (even like KeyError or ValueError), then you can try to do this:

    try:
        # ... some external module call ...
    except:
        # ... some exception handling ...
    

    (preferably for single calls, not for bigger blocks that are more likely to contain your module errors)

But you should be aware that this will not give you enough information in case of error (eg. you will have to guess that the function you are trying to call does not exist there).

Also make sure there is no other way to solve your problem. You are dangerously close to implementing Diaper Pattern and should avoid that by giving explicit classes for the exceptions you want to catch.

like image 122
Tadeck Avatar answered Oct 25 '22 15:10

Tadeck


Don't do this in code you don't want to be embarrassed by, but this works:

import requests, inspect

exception_types = [obj for name, obj in inspect.getmembers(requests.exceptions) if inspect.isclass(obj) and issubclass(obj, Exception)]

try:
    raise requests.exceptions.HTTPError()
except Exception as e:
    if any(issubclass(e.__class__, lv) for lv in exception_types):
        print "This exception came from requests.exceptions"
    else:
        print "This exception came from somewhere else"
        raise
like image 35
kuzzooroo Avatar answered Oct 25 '22 17:10

kuzzooroo