I'm trying to catch all the exceptions at once

Asked 1 years ago, Updated 1 years ago, 115 views

try:
    #What kind of chords?
except IOError:
    #Exception Processing

It's not about every one of the exceptions, but just catch all the exceptions at once

I'd like to take care of it. Is it possible?

exception-handling python

2022-09-22 13:16

1 Answers

You can squeeze it like this if you want. (The code below also handles keyboard interrupts such as sigint)

try:
    do_something()
except:
    print "I got you!"

Codes that handle all exceptions at once are not recommended by Python. (It's okay if you raise it again in the exception as below.))

try:
    do_something()
except:
    raise

I don't know why you want to catch exceptions at once, but if you do it for logging,

import traceback
import logging

try:
    do_something()
except Exception as e:
    logging.error(traceback.format_exc()) #Logging

You can share it.


2022-09-22 13:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.