Movatterモバイル変換


[0]ホーム

URL:


Sorry, we no longer support your browser
Please upgrade toMicrosoft Edge,Google Chrome, orFirefox. Learn more about ourbrowser support.
Skip to main content
Stack Overflow
  1. About
  2. For Teams
Loading…
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Return to Answer

Show exception syntax for both Python 2 and Python 3.
bignose
  • 32.7k
  • 16
  • 80
  • 116

Re-raising exceptions:

# Python 2 syntaxtry:    some_operation()except SomeError, e:    if is_fatal(e):        raise    handle_nonfatal(e)# Python 3 syntaxtry:    some_operation()except SomeError as e:    if is_fatal(e):        raise    handle_nonfatal(e)

The 'raise' statement with no arguments inside an error handler tells Python to re-raise the exceptionwith the original traceback intact, allowing you to say "oh, sorry, sorry, I didn't mean to catch that, sorry, sorry."

If you wish to print, store or fiddle with the original traceback, you can get it with sys.exc_info(), and printing it like Python would is done with the 'traceback' module.

Re-raising exceptions:

try:    some_operation()except SomeError, e:    if is_fatal(e):        raise    handle_nonfatal(e)

The 'raise' statement with no arguments inside an error handler tells Python to re-raise the exceptionwith the original traceback intact, allowing you to say "oh, sorry, sorry, I didn't mean to catch that, sorry, sorry."

If you wish to print, store or fiddle with the original traceback, you can get it with sys.exc_info(), and printing it like Python would is done with the 'traceback' module.

Re-raising exceptions:

# Python 2 syntaxtry:    some_operation()except SomeError, e:    if is_fatal(e):        raise    handle_nonfatal(e)# Python 3 syntaxtry:    some_operation()except SomeError as e:    if is_fatal(e):        raise    handle_nonfatal(e)

The 'raise' statement with no arguments inside an error handler tells Python to re-raise the exceptionwith the original traceback intact, allowing you to say "oh, sorry, sorry, I didn't mean to catch that, sorry, sorry."

If you wish to print, store or fiddle with the original traceback, you can get it with sys.exc_info(), and printing it like Python would is done with the 'traceback' module.

Post Made Community Wiki byCommunityBot
Thomas Wouters
  • 134.2k
  • 23
  • 153
  • 123

Re-raising exceptions:

try:    some_operation()except SomeError, e:    if is_fatal(e):        raise    handle_nonfatal(e)

The 'raise' statement with no arguments inside an error handler tells Python to re-raise the exceptionwith the original traceback intact, allowing you to say "oh, sorry, sorry, I didn't mean to catch that, sorry, sorry."

If you wish to print, store or fiddle with the original traceback, you can get it with sys.exc_info(), and printing it like Python would is done with the 'traceback' module.

lang-py

[8]ページ先頭

©2009-2025 Movatter.jp