Python Forum
Catching a crash within a library code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Catching a crash within a library code
#1
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
Reply
#2
That should work if the OSError is the exception thrown. Can you show the actual code and a traceback where it doesn't work?
Reply
#3
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))
Reply
#4
(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.
Reply
#5
(Nov-21-2021, 10:58 PM)BashBedlam Wrote: If you do this:
Thanks but I cannot. I'm dealing with a microcontroller.
Reply
#6
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.
ebolisa likes this post
Reply
#7
(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***
Reply
#8
That the second one is caught seems odd. How about this?

...
except Exception as e:
    print(repr(e))
    print(type(e))
Reply
#9
Now this thread makes me wonder whether it would be possible to catch an exception if the library code does not propagate it.
Reply
#10
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I open the Source code of a library? JaneTan 1 516 Aug-18-2021, 02:12 AM
Last Post: Larz60+
  Modbus-tk library catching modbus-tk exception zazas321 1 825 Nov-10-2020, 03:06 PM
Last Post: Larz60+
  Python 3 crash course james_newbie 6 2,038 Aug-25-2019, 08:23 PM
Last Post: snippsat
  Catching Errors Alienspecimen 1 1,436 May-18-2019, 11:07 PM
Last Post: Larz60+
  Pexpect not catching embeded ssh response luchoArg32 0 1,412 Feb-08-2019, 08:45 AM
Last Post: luchoArg32
  catching / handle index out of range 3Pinter 3 1,581 Feb-04-2019, 01:19 PM
Last Post: buran
  Catching exceptions in embedded code no longer works on 3.7.2? FFMG 5 1,766 Feb-02-2019, 10:15 AM
Last Post: Larz60+
  win32com Events not catching dageci 0 2,422 Aug-06-2018, 03:18 PM
Last Post: dageci
  How to Crash a Computer keegan_010 6 2,602 Aug-05-2018, 06:01 AM
Last Post: Skaperen
  Crash Course Chapter 7 Demini 3 2,276 Feb-05-2018, 10:58 PM
Last Post: Demini

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020