Python Forum

Full Version: Catching a crash within a library code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

How can I catch errors within a library that I cannot modify?
The following code doesn't catch it.
TIA

import some_lib

try:
    #do some with the library
except OSError as e:
    print('*** ERROR ***', str(e))  #<--- the lib code crushes but this line is not reached
That should work if the OSError is the exception thrown. Can you show the actual code and a traceback where it doesn't work?
If you do this:
import numpy
array = numpy.array ([one, two, three])
It will throw this error:
Error:
Traceback (most recent call last): File "tester.py", line 15, in <module> array = numpy.array ([one, two, three]) NameError: name 'one' is not defined
That last line and in particular
Error:
NameError
is the information you need to use the try/except block. Try this:
import numpy
try :
	array = numpy.array ([one, two, three])
except NameError as e :
	print('*** ERROR ***', str(e))
(Nov-21-2021, 10:48 PM)bowlofred Wrote: [ -> ]That should work if the OSError is the exception thrown. Can you show the actual code and a traceback where it doesn't work?
code which is the culprit:
try:
    blynk = BlynkLib.Blynk(BLYNK_AUTH,
                           insecure=False,          # disable SSL/TLS
                           server='blynk.cloud',   # set server address
                           port=443,                  # set server port
                           heartbeat=30,            # set heartbeat to 30 secs
                           log=False                   # use print function for debug logging
                           )
except OSError as e:
    print('ERROR***', e)
    #machine.reset()
error:
Error:
Connecting to blynk.cloud:443... Blynk disconnected Connecting to fra1.blynk.cloud:443... Traceback (most recent call last): File "<stdin>", line 109, in <module> File "<stdin>", line 103, in runLoop File "BlynkLib.py", line 261, in run File "BlynkLib.py", line 202, in process File "BlynkLib.py", line 68, in emit File "BlynkLib.py", line 221, in redirect File "BlynkLib.py", line 240, in connect OSError: [Errno 12] ENOMEM
There's an intentional error in the code which is cousing the lib to crush.
I just want to know why my print line is not reached or how to fix it. Because, in a eventual real crush, the PLC/microcontroller needs to reset.
Thanks.
(Nov-21-2021, 10:58 PM)BashBedlam Wrote: [ -> ]If you do this:
Thanks but I cannot. I'm dealing with a microcontroller.
Looks correct to me. Two things to try:

Make sure that the code logic and micropython aren't doing anything odd here. This should be caught as well:
try:
    raise OSError('Test error')
    blynk = BlynkLib.Blynk(BLYNK_AUTH,
                           insecure=False,          # disable SSL/TLS
                           server='blynk.cloud',   # set server address
                           port=443,                  # set server port
                           heartbeat=30,            # set heartbeat to 30 secs
                           log=False                   # use print function for debug logging
                           )
except OSError as e:
    print('ERROR***', e)
    #machine.reset()
And:
try:
    blynk = BlynkLib.Blynk(BLYNK_AUTH,
                           insecure=False,          # disable SSL/TLS
                           server='blynk.cloud',   # set server address
                           port=443,                  # set server port
                           heartbeat=30,            # set heartbeat to 30 secs
                           log=False                   # use print function for debug logging
                           )
except:
    print('ERROR***')
    #machine.reset()
I see no reason that the OSError shouldn't be caught, but this test should catch any possible exception. Maybe try them and see if they are both caught or not.
(Nov-21-2021, 11:47 PM)bowlofred Wrote: [ -> ]I see no reason that the OSError shouldn't be caught, but this test should catch any possible exception. Maybe try them and see if they are both caught or not.
Interesting: both codes catch the error. Thanks!
ERROR*** Test error
ERROR***
That the second one is caught seems odd. How about this?

...
except Exception as e:
    print(repr(e))
    print(type(e))
Now this thread makes me wonder whether it would be possible to catch an exception if the library code does not propagate it.
If the library were to catch the exception and not re-raise it, then there would be nothing for the library caller to catch. I think the library has 3 choices:

* Not catch the error (or raise its own error). These should be catchable by the caller.
* Catch the error and perform a normal return. There's nothing for the caller to catch. It has to interpret the return value to determine success or problem or other.
* Kill the interpreter via os._exit(). Nasty and shouldn't be done. The caller will not execute at all (and can do no cleanup).