Catch only some runtime errors in Python

I import a module that causes some errors under certain conditions: RuntimeError: pyparted requires root access

I know that I can just check root access before importing, but I would like to know how to catch this spesific type of error using the try / except statement for a future reference. Is there a way to distinguish between this RuntimeError and others that can be raised?

+6
python exception-handling try-catch runtime-error
source share
6 answers

I know that I can just check root access before importing, but I would like to know how to catch this spesific type of error using the try / except statement for a future reference. Is there a way to distinguish between this RuntimeError and others that can be raised?

If the error is caused by a specific condition, then I think the easiest way to catch the error is to check the condition, and you can create a more specific error yourself. Indeed, an β€œerror” exists before an error occurs, since in this case it is a problem with the environment.

I agree with the above. Matching text on a bug is a frightening prospect.

+7
source share

You can check exception attributes to distinguish from other possible RuntimeError exceptions. For example, raise the error again if it does not match the predefined text message.

  try: import pypatred except RuntimeError,e: if e.message == 'RuntimeError: pyparted requires root access': return 'pyparted - no root access' raise 

Of course, a direct comparison of the text is just an example, you can search for included substrings or regular expressions.

It should be noted that the .message attribute of exceptions has been deprecated since Python 2.6 . Text can be found in .args , usually args[0] .

... For 2.6, the message attribute is deprecated in favor of the args attribute.

+8
source share
 try: import pyparted except RuntimeError: print('RuntimeError is raised') raise 

more detailed exception handling in the textbook .

This situation should make ImportError , in my opinion. And you can do it yourself:

 try: import pyparted except RuntimeError as e: raise ImportError(e) 
+4
source share

Yes.

  try: import module except RuntimeError: pass 

import is interpreted as any other statement, they are not special. You can do

 if condition: import module 
+1
source share
 try: import ... except RuntimeError: # Do something 
+1
source share

RuntimeError Raised when an error is detected that does not fall into any of the other categories.

 def foo(): try: foo() except RuntimeError, e: print e print " Runtime Error occurred due to exceeded maximum recursion depth " 

Here's how we catch a RuntimeError caused by exceeding the recursion limit in python

And if you want to call your function by recursion limit, you can do the following

 import sys def foo(): try: foo() except RuntimeError, e: sys.setrecursionlimit(1200) foo() 

But it is always not recommended to change the recursion limit, but very small changes are allowed in the recursion limit

0
source share

All Articles