Python and checked exceptions

P

Peter Otten

Kay said:
A new cookbook recipe suggesting two decorators @throws and @catches
for treatment of checked exceptions in Python:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498131

This might be of practical interest for some and theoretical interest
for others - in particular those who know checked ex as a language
feature of Java.

Probably not what you wanted:
Raises UncheckedExceptionError(exceptions.ZeroDivisionError) -> OK
Raises ZeroDivisionError -> OK
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "checked_exceptions.py", line 134, in test
test4(3,2)
UnboundLocalError: local variable 'test4' referenced before assignment

Peter
 
L

Lawrence D'Oliveiro

A new cookbook recipe suggesting two decorators @throws and @catches
for treatment of checked exceptions in Python:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498131

This might be of practical interest for some and theoretical interest
for others - in particular those who know checked ex as a language
feature of Java.

The trouble with having to declare every possible exception that a function
might throw is that it rapidly turns into a complete mess. That's why such
declarations are optional in C++, and only semi-mandatory in Java.
 
D

Diez B. Roggisch

The trouble with having to declare every possible exception that a
function might throw is that it rapidly turns into a complete mess. That's
why such declarations are optional in C++, and only semi-mandatory in
Java.

I agree with you that they are a mess, and I usually wrap them
RuntimeExceptions.

Why do you call them semi-mandatory?

diez
 
K

Kay Schluehr

Peter said:
Probably not what you wanted:

Raises UncheckedExceptionError(exceptions.ZeroDivisionError) -> OK
Raises ZeroDivisionError -> OK
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "checked_exceptions.py", line 134, in test
test4(3,2)
UnboundLocalError: local variable 'test4' referenced before assignment

Peter

Thanks, I fixed this.

Kay
 

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

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top