1

I'm really new in Python and a have no experience with exceptions but I've read all the documentation and couldn't find an answer ... so I'm looking for a deeper view in except's semantics. When we have for example:

try:
    x = 2
except GreaterThanOne:
    print("The value is greater than one")

In this case I want the message to be printed.Is there a way for the GreaterThanOne class(exception) to be defined to raise when the entered value is greater than one ?


Ok, let me be more specific ...

Every error raises by a specific rule which should be add in the error attributes, am I right ? For example:

try:
    myvalue = x / y
except ZeroDivisionError:
    print("Some error message printed ...")

So when I use this code and enter for y to be 0 the exception ZeroDivisionError will raise ... Can I for example redefine ZeroDivisionError to raise like this but if y is set to be ... 2 or 3 or any other value ?

Input:

x = 10
y = 2
try:
    myvalue = x / y
except ZeroDivisionError:
    print("division by 2")

Output: division by 2

2 Answers 2

4

Here's an example that should help you understand. Run this in your Python interpreter and watch how the exception is raised and caught (or not caught) when you call set_val(2).

# Defining our Exception subclass:
class GreaterThanOne(Exception):
    pass

# The global value we pretend to care about:
val = 0

# Function to set a value but possibly raise our new Exception 
def set_val(new_val):
    if new_val > 1:
        raise GreaterThanOne("%d > 1" % new_val)
    val = new_val

# Catching exception:
try:
    set_val(0)
    set_val(1)
    set_val(2)
except GreaterThanOne:
    print "Whoops - one of those values was greater than one"

# Not catching exception:
set_val(0)
set_val(1)
set_val(2)
set_val(3)
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer helps but is there an attribute in the exception class in which the except look for(which could be a bool expression) when having try ... except ?
Try to rephrase your question... What are you trying to accomplish?
0

an try-except block catches exception in this block.

try:
    #some stuff here
except ExceptionClass as e:
    #Exception handling here

the class after the except keyword indicates which kind of exception you want to catch. Usually you give a specific class, like ValueError or KeyError. You can also use the Exception class, to catch any exception. Because all the other exceptionclasses inhert from Exception.

so if you want to use this construct, an exception needs to be raised, Either by a function / method you call, or you raise it yourself with the raise keyword.

like this:

try:
    raise KeyError('Just for test')
except KeyError as e:
    #Exception handling here

The try except doesn't automagically inspect the whole code between it, it just looks for exceptions... Or to be more specific, it looks for those exceptions you tell it to look for.

Of course you can also inspect the exception instance.

try:
    raise KeyError('Just for test')
except KeyError as e:
    print e.args

For more information, please see: http://docs.python.org/2/tutorial/errors.html

1 Comment

The except KeyError, e syntax has been deprecated for a while in favour of except KeyError as e. This is especially relevant since the OP appears to be using print as a function, and is therefore probably on Python 3 where the comma form is a syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.