-1

I am used to the following in Java:

try {
    // something bad happens here
} catch (SomeException se) {
   se.printStackTrace(); // let's me print the stack trace
   System.out.println(se); // let's me print and inspect the exception
   throw se; // I can re-throw it or wrap it and throw it
}

I am having a really hard time linking this to Pythons

try:
    pass # do something 
except ExceptionClass1:
    print("OK I know I caught an ExceptionClass1 type exception")
    # but how do I get to the stack trace
    # how do I get to the object of the exception
7
  • 1
    Have you checked out the Python tutorial section on Errors and Exceptions? docs.python.org/3/tutorial/errors.html Commented Sep 15, 2021 at 1:58
  • 1
    What do you mean by "how does it compare"? If the question is how to do specific things, then you should specifically ask those questions, and ask them one at a time, and only after you have done the expected research. For example, try putting python print exception stack trace into a search engine, or python exception handling tutorial. Commented Sep 15, 2021 at 1:59
  • If you want to know the basic syntax for a language, you should be working through a tutorial or the language documentation, not asking people on Stack Overflow to "translate" from one language to another for you. This is not a discussion forum. Commented Sep 15, 2021 at 2:00
  • @KarlKnechtel thank you for that input -- I really have and still don't find something that really helped. Commented Sep 15, 2021 at 22:34
  • I found this previous Stack Overflow question as the third result for python print exception stack trace. The second result was the official documentation for the traceback standard library module, as in the answer you accepted. The first result is from a tutorial site with worked examples and detailed explanation. How did any of those fail to help you? Commented Sep 16, 2021 at 5:41

1 Answer 1

1

You will need the traceback library:

try:
    # Do something
except ExceptionClass1 as error:
    traceback.print_exc()  # Prints the exception

    # Raise alone will re-raise the same exception
    raise              

    # Or, raise a new exception
    raise RuntimeError("bad things") from error
Sign up to request clarification or add additional context in comments.

1 Comment

Might want to explicitly call out the change to add as error so the exception is captured to a local variable name (it's available from sys.exc_info() without that, but manual use of sys.exc_info() is largely unnecessary in modern Python). Also note: raise RuntimeError("bad things") from error is redundant; raising a new exception in an except block automatically chains the exception being handled to it exactly the same way from error is doing it explicitly. from is mostly important when using from None to explicitly suppress exception chaining.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.