1

I'm better versed in C# than Python.

In C# I can catch all exceptions as default and handle them like showing a error window or write them to a log file.

What about Python? I can't find a default statement, so that I could write all occurred exceptions to a log file and continue with the program.

For example, I want to catch all types of exceptions. In C# it's like this

                try
                {
                   do something.....
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(),
                    @"Fehler",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                    );
                    return false;
                }

It's not necessary to know what kind of exception the program is throwing. How can I do this in Python?

halfer
  • 19,824
  • 17
  • 99
  • 186
tux007
  • 282
  • 1
  • 3
  • 17
  • Possible duplicate of [Generic Exception Handling in Python the "Right Way"](http://stackoverflow.com/questions/129144/generic-exception-handling-in-python-the-right-way) – NicoRiff Feb 19 '17 at 01:23

2 Answers2

1

For default exception handling:

except:
    print("Unexpected error:", sys.exc_info()[0])

https://docs.python.org/3/library/sys.html#sys.exc_info

As ZeroPiraeus point in comment it could be dangerous to use this as it can provide a unwanted behavior in some situations.

Pradeep
  • 3,258
  • 1
  • 23
  • 36
Logman
  • 4,031
  • 1
  • 23
  • 35
  • 1
    No, you absolutely don't. At a *bare minimum*, you specify `except Exception:` to avoid also catching `SystemExit`, `KeyboardInterrupt` etc., and in almost all cases you avoid that too. – Zero Piraeus Feb 19 '17 at 01:56
  • @ZeroPiraeus you don't do what? You can't write this? Or in your opinion he shouldn't? I can say the same of his code he shouldn't catch general exception in c# but he does. It is not my role to judge his programing style as he could be more superior then me in it. My role is to answer his question. – Logman Feb 19 '17 at 02:07
  • Bare `except:` clauses are a very well-known source of hard-to-trace errors, and are universally discouraged by experienced Python programmers. It's irresponsible to answer an unwitting "how do I shoot myself in the foot?" question by showing OP how to disengage the safety, unless you at the very least offer a basic caution against doing so. – Zero Piraeus Feb 19 '17 at 02:35
  • @ZeroPiraeus I agree with you but this is true for most languages and probability that someone familiar with c# is not aware of this issue is low. But I will add warning to my answer. – Logman Feb 19 '17 at 02:54
  • thanks, for your comments, I learned something about catching all exceptions ;) – tux007 Feb 20 '17 at 01:46
0

From the python documentation on errors you handle errors in python using try/except:

>>> while True:
...     try:
...         x = int(input("Please enter a number: "))
...         break
...     except ValueError:
...         print("Oops!  That was no valid number.  Try again...")
...
krock
  • 28,904
  • 13
  • 79
  • 85