does python has its error report message like $! in perl

perl, python

Solution

Python generally uses exceptions to report errors. If some OS operation returns an error code, it raises an exception that you catch in a try-except block. For OS operations, that is OSError. The errno is contained in the exception instance.

from __future__ import print_function
import os

try:
        os.stat("xxx")
except OSError as err:
        print (err)
        # The "err" object is on instance of OSError. It supports indexing, with the first element as the errno value.
        print(err[0])

Output:

[Errno 2] No such file or directory: 'xxx'
2

Problem

I am wondering if python has its error report message equivalent to $! in perl ? Anyone who could give me an answer will be greatly appreciated. Added: ``` example% ./test File "./test", line 7 test1 = test.Test(dir) ^ SyntaxError: invalid syntax ``` When Exception occurs, I got something like this. If I apply try and catch block, I can catch it and use sys.exit(message) to log the message. But, is there any chance that I can get the string SyntaxError: invalid syntax and put it in message

Original source