Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to report an exception for later

I have a python file in which i have two functions that each of them raise an exception.

def f():
    raise e1

def g():
    raise e2

My question, is it possible to store these exceptions in a variable, like list for example--[e1, e2]--, in order to control the order of exceptions execution in another function, say h ?

like image 274
LChampo Avatar asked Jul 31 '18 13:07

LChampo


People also ask

What is an exception report?

What Is Exception Reporting? In a nutshell, an exception report identifies transactions or performance metrics where actual outcomes deviate significantly from expectations, and flags those outliers for follow-up and resolution.

How do I raise exceptions to messages?

The raise keyword is used to raise an exception. You can define what kind of error to raise, and the text to print to the user.

How do you raise an exception in the classroom?

The syntax is: RAISE EXCEPTION TYPE cx_... [EXPORTING f 1 = a 1 f 2 = a 2 ...]. This statement raises the exception linked to exception class cx_... and generates a corresponding exception object.

What happens when exception is thrown?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.


1 Answers

Exceptions are objects, like most things in Python; specifically, you can bind one to a name when you catch it, then add it to a list. For example:

exceptions = []
try:
    f()
except Exception as f_exc:
    exceptions.append(f_exc)

try:
    g()
except Exception as g_exc:
    exceptions.append(g_exc)

I'm not sure what use case you have in mind that you want to store exceptions to look at later. Typically, you act on an exception as soon as you catch it.

like image 87
chepner Avatar answered Sep 23 '22 01:09

chepner