How to use "raise" keyword in Python

The Solution to How to use "raise" keyword in Python is


It has 2 purposes.

yentup has given the first one.

It's used for raising your own errors.

if something:
    raise Exception('My error!')

The second is to reraise the current exception in an exception handler, so that it can be handled further up the call stack.

try:
  generate_exception()
except SomeException as e:
  if not can_handle(e):
    raise
  handle_exception(e)

~ Answered on 2012-12-19 17:32:03


Most Viewed Questions: