-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
user1172468
  • 5,306
  • 6
  • 35
  • 62
  • 1
    Have you checked out the Python tutorial section on Errors and Exceptions? https://docs.python.org/3/tutorial/errors.html – djs Sep 15 '21 at 01: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](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). For example, try putting `python print exception stack trace` into a search engine, or `python exception handling tutorial`. – Karl Knechtel Sep 15 '21 at 01: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. – Karl Knechtel Sep 15 '21 at 02:00
  • @KarlKnechtel thank you for that input -- I really have and still don't find something that really helped. – user1172468 Sep 15 '21 at 22:34
  • I found [this previous Stack Overflow question](https://stackoverflow.com/q/52742612/) as the [third result](https://duckduckgo.com/?q=python+print+exception+stack+trace) for `python print exception stack trace`. The second result was [the official documentation](https://docs.python.org/3/library/traceback.html) for the `traceback` standard library module, as in the answer you accepted. The [first result](https://www.geeksforgeeks.org/how-to-print-exception-stack-trace-in-python/) is from a tutorial site with worked examples and detailed explanation. How did any of those fail to help you? – Karl Knechtel Sep 16 '21 at 05:41
  • You have multiple gold badges and an account over 9 years old. You should understand how to take these basic steps to help yourself. If you actually did, then you **at least** should know better to tell us *specifically* the steps you took (i.e. specific queries that you tried in a search engine), *specifically* what you found and *specifically* why it was not helpful. – Karl Knechtel Sep 16 '21 at 05:42
  • @KarlKnechtel - you are I could have listed all the references that I visited all the examples -- which I do and used to -- over the nine years I feel that is a public shaming exercise: look I research, I read, I do this and that -- nobody reads them but I guess its ritual the community exercises -- I did dare a bit this time to dispensed with that. At worst my question will be closed and I will be explicitly shamed. – user1172468 Sep 16 '21 at 12:42

1 Answers1

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
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • 1
    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. – ShadowRanger Sep 15 '21 at 02:16