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'?
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!”)
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.
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.
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.
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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With