Why exception doesn't supress ssl.SSLError
Question:
I want to supress all expection by calling:
try:
doSomething()
doSomething2() # exception is raised
except Exception:
print("expection thrown, test should pass!")
In doSomething2() there are some exceptions raised like
E ssl.SSLError: [SSL: NO_CIPHERS_AVAILABLE] no ciphers available (_ssl.c:1076)
Why except Exception
doesn’t supress all exceptions ?
Answers:
So we have the following code:
try:
doSomething()
doSomething2() # expection is raised
except Exception:
print("expection thrown, test should pass!")
and … apparently … the exception handler is not catching ssl.SSLError.
How can this be? After all SSLError
is a subclass of OSError
which is in turn a subclass of Exception
. So, the handler should catch an SSLError
.
Here are all of the possible explanations that I can think of:
-
SSLError
is not thrown at all. We can exclude that as you have shown us evidence that it is thrown.
-
The SSLError
is thrown but caught further up the stack. For example, if doSomething2()
does something like this:
def something2():
...
try:
...
# attempt to open an HTTPS connection
...
catch OSError:
# report the exception
That exception handler will catch the SSLError
… and the outer handler won’t ever see it or be able to suppress it.
-
The SSLError
is thrown on a different thread stack. Uncaught exceptions thrown by one thread don’t automatically propagate to a different stack. Instead, they will just terminate the thread that threw the exception.
-
Something has monkey-patched SSLError
to change its superclass. This seems so perverse that I’d call this implausible. But it may be technically possible.
I want to supress all expection by calling:
try:
doSomething()
doSomething2() # exception is raised
except Exception:
print("expection thrown, test should pass!")
In doSomething2() there are some exceptions raised like
E ssl.SSLError: [SSL: NO_CIPHERS_AVAILABLE] no ciphers available (_ssl.c:1076)
Why except Exception
doesn’t supress all exceptions ?
So we have the following code:
try:
doSomething()
doSomething2() # expection is raised
except Exception:
print("expection thrown, test should pass!")
and … apparently … the exception handler is not catching ssl.SSLError.
How can this be? After all SSLError
is a subclass of OSError
which is in turn a subclass of Exception
. So, the handler should catch an SSLError
.
Here are all of the possible explanations that I can think of:
-
SSLError
is not thrown at all. We can exclude that as you have shown us evidence that it is thrown. -
The
SSLError
is thrown but caught further up the stack. For example, ifdoSomething2()
does something like this:def something2(): ... try: ... # attempt to open an HTTPS connection ... catch OSError: # report the exception
That exception handler will catch the
SSLError
… and the outer handler won’t ever see it or be able to suppress it. -
The
SSLError
is thrown on a different thread stack. Uncaught exceptions thrown by one thread don’t automatically propagate to a different stack. Instead, they will just terminate the thread that threw the exception. -
Something has monkey-patched
SSLError
to change its superclass. This seems so perverse that I’d call this implausible. But it may be technically possible.