Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print an exception in Python 3?

Right now, I catch the exception in the except Exception: clause, and do print(exception). The result provides no information since it always prints <class 'Exception'>. I knew this used to work in python 2, but how do I do it in python3?

like image 432
Haonan Chen Avatar asked Jan 11 '17 17:01

Haonan Chen


People also ask

How do you print exception in Python?

If you are going to print the exception, it is better to use print(repr(e)) ; the base Exception. __str__ implementation only returns the exception message, not the type. Or, use the traceback module, which has methods for printing the current exception, formatted, or the full traceback.

How do I print an exception?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How do I get exception details in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.


1 Answers

I'm guessing that you need to assign the Exception to a variable. As shown in the Python 3 tutorial:

def fails():     x = 1 / 0  try:     fails() except Exception as ex:     print(ex) 

To give a brief explanation, as is a pseudo-assignment keyword used in certain compound statements to assign or alias the preceding statement to a variable.

In this case, as assigns the caught exception to a variable allowing for information about the exception to stored and used later, instead of needing to be dealt with immediately.

(This is discussed in detail in the Python 3 Language Reference: The try Statement.)


There are other compound statements that use as. The first is the with statement:

@contextmanager def opening(filename):     f = open(filename)     try:         yield f     finally:         f.close()  with opening(filename) as f:     # ...read data from f... 

Here, with statements are used to wrap the execution of a block with methods defined by context managers. This functions like an extended try...except...finally statement in a neat generator package, and the as statement assigns the generator-produced result from the context manager to a variable for extended use.

(This is discussed in detail in the Python 3 Language Reference: The with Statement.)


As of Python 3.10, match statements also use as:

from random import randint  match randint(0, 2):     case 0|1 as low:         print(f"{low} is a low number")     case _:         print("not a low number") 

match statements take an expression (in this case, randint(0, 2)) and compare its value to each case branch one at a time until one of them succeeds, at which point it executes that branch's block. In a case branch, as can be used to assign the value of the branch to a variable if that branch succeeds. If it doesn't succeed, it is not bound.

(The match statement is covered by the tutorial and discussed in detail in the Python 3 Language Reference: match Statements.)


Finally, as can be used when importing modules, to alias a module to a different (usually shorter) name:

import foo.bar.baz as fbb 

This is discussed in detail in the Python 3 Language Reference: The import Statement.

like image 92
Noah Bogart Avatar answered Sep 24 '22 05:09

Noah Bogart