Skip to main content
Option to add note to exception.
Source Link
Karel Marik
  • 1.1k
  • 11
  • 16

I believe a better idea isThere are several methods to useresolve it:

1. Exception Chain

2. Suppress Original Failure

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 7
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from None

TypeError: Apology error

3. Add Notes To Exception

You can also add explanation notes to original exception

def failure(i):
    raise ValueError(f"Real error in failure method.")

try:
    a = 3.1415
    failure(a)
except ValueError as ex:
    ex.add_note(f'The "{ex.args[0]}" happened with argument {a} when invoked from main script.')
    ex.add_note('Final note: bye')
    raise

which yields ...

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[14], line 6
      4 try:
      5     a = 3.1415
----> 6     failure(a)
      7 except ValueError as ex:
      8     ex.add_note(f'The "{ex.args[0]}" happened with argument {a} when invoked from main script.')

Cell In[14], line 2, in failure(i)
      1 def failure(i):
----> 2     raise ValueError(f"Real error in failure method.")

ValueError: Real error in failure method.
The "Real error in failure method." happened with argument 3.1415 when invoked from main script.
Final note: bye

I believe a better idea is to use

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 7
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from None

TypeError: Apology error

There are several methods to resolve it:

1. Exception Chain

2. Suppress Original Failure

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 7
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from None

TypeError: Apology error

3. Add Notes To Exception

You can also add explanation notes to original exception

def failure(i):
    raise ValueError(f"Real error in failure method.")

try:
    a = 3.1415
    failure(a)
except ValueError as ex:
    ex.add_note(f'The "{ex.args[0]}" happened with argument {a} when invoked from main script.')
    ex.add_note('Final note: bye')
    raise

which yields ...

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[14], line 6
      4 try:
      5     a = 3.1415
----> 6     failure(a)
      7 except ValueError as ex:
      8     ex.add_note(f'The "{ex.args[0]}" happened with argument {a} when invoked from main script.')

Cell In[14], line 2, in failure(i)
      1 def failure(i):
----> 2     raise ValueError(f"Real error in failure method.")

ValueError: Real error in failure method.
The "Real error in failure method." happened with argument 3.1415 when invoked from main script.
Final note: bye
added 688 characters in body
Source Link
Karel Marik
  • 1.1k
  • 11
  • 16

I believe a better idea is to use

raise NewException("Explain why") from CatchedException

pattern. In particular, considering Python 3 and the example given by @BrenBarn I use following

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from ex

which yields

--------- ValueError----                                
Traceback (most recent call last) 
      4 try:
----> 5     failure()
      6 except ValueError as ex:

      1 def failure():
----> 2     raise ValueError("Real error")
      3 

ValueError: Real error

The above exception was the direct cause of the following exception:

-----TypeError-----
Traceback (most recent call last) 
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from ex

TypeError: Apology error

You can also suppress the original exception context by raise the new exception from None. The code will change to

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from None

.. and it yields following output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 7
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from None

TypeError: Apology error

I believe a better idea is to use

raise NewException("Explain why") from CatchedException

pattern. In particular, considering Python 3 and the example given by @BrenBarn I use following

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from ex

which yields

--------- ValueError----                                
Traceback (most recent call last) 
      4 try:
----> 5     failure()
      6 except ValueError as ex:

      1 def failure():
----> 2     raise ValueError("Real error")
      3 

ValueError: Real error

The above exception was the direct cause of the following exception:

-----TypeError-----
Traceback (most recent call last) 
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from ex

TypeError: Apology error

I believe a better idea is to use

raise NewException("Explain why") from CatchedException

pattern. In particular, considering Python 3 and the example given by @BrenBarn I use following

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from ex

which yields

--------- ValueError----                                
Traceback (most recent call last) 
      4 try:
----> 5     failure()
      6 except ValueError as ex:

      1 def failure():
----> 2     raise ValueError("Real error")
      3 

ValueError: Real error

The above exception was the direct cause of the following exception:

-----TypeError-----
Traceback (most recent call last) 
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from ex

TypeError: Apology error

You can also suppress the original exception context by raise the new exception from None. The code will change to

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from None

.. and it yields following output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 7
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from None

TypeError: Apology error
deleted 270 characters in body
Source Link
Karel Marik
  • 1.1k
  • 11
  • 16

I believe a better idea is to use

raise NewException("Explain why") from CatchedException

pattern. In particular, considering Python 3 and the example given by @BrenBarn I use following

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from ex

which yields

----------------------------------------------------------------------- ValueError---- ValueError                                 
Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_15716\1525308893.py in <module>
      4 try:
----> 5     failure()
      6 except ValueError as ex:

~\AppData\Local\Temp\ipykernel_15716\1525308893.py in failure()
      1 def failure():
----> 2     raise ValueError("Real error")
      3 

ValueError: Real error

The above exception was the direct cause of the following exception:

-----TypeError                                 -----
Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_15716\1525308893.py in <module>
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from ex

TypeError: Apology error

I believe a better idea is to use

raise NewException("Explain why") from CatchedException

pattern. In particular, considering Python 3 and the example given by @BrenBarn I use following

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from ex

which yields

--------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_15716\1525308893.py in <module>
      4 try:
----> 5     failure()
      6 except ValueError as ex:

~\AppData\Local\Temp\ipykernel_15716\1525308893.py in failure()
      1 def failure():
----> 2     raise ValueError("Real error")
      3 

ValueError: Real error

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_15716\1525308893.py in <module>
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from ex

TypeError: Apology error

I believe a better idea is to use

raise NewException("Explain why") from CatchedException

pattern. In particular, considering Python 3 and the example given by @BrenBarn I use following

def failure():
    raise ValueError("Real error")

try:
    failure()
except ValueError as ex:
    raise TypeError("Apology error") from ex

which yields

--------- ValueError----                                 
Traceback (most recent call last) 
      4 try:
----> 5     failure()
      6 except ValueError as ex:

      1 def failure():
----> 2     raise ValueError("Real error")
      3 

ValueError: Real error

The above exception was the direct cause of the following exception:

-----TypeError-----
Traceback (most recent call last) 
      5     failure()
      6 except ValueError as ex:
----> 7     raise TypeError("Apology error") from ex

TypeError: Apology error
Source Link
Karel Marik
  • 1.1k
  • 11
  • 16
Loading