lfaraone
lfaraone

Reputation: 50672

Catch only some runtime errors in Python

I'm importing a module which raises the following error in some conditions: RuntimeError: pyparted requires root access

I know that I can just check for root access before the import, but I'd like to know how to catch this spesific kind of error via a try/except statement for future reference. Is there any way to differentiate between this RuntimeError and others that might be raised?

Upvotes: 10

Views: 37903

Answers (6)

neotam
neotam

Reputation: 2731

RuntimeError Raised when an error is detected that doesn’t fall in any of the other categories

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

That is how we will catch the RuntimeError caused by the exceeded recursion limit in python

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

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

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

Upvotes: 2

decitrig
decitrig

Reputation: 788

I know that I can just check for root access before the import, but I'd like to know how to catch this spesific kind of error via a try/except statement for future reference. Is there any way to differentiate between this RuntimeError and others that might be raised?

If the error is caused by a specific condition, then I think the easiest way to catch the error is to test for the condition, and you can raise a more specific error yourself. After all the 'error' exists before the error is thrown, since in this case its a problem with the environment.

I agree with those above - text matching on an error is kind of a terrifying prospect.

Upvotes: 8

gimel
gimel

Reputation: 86344

You can check attributes of the exception to differentiate from other possible RuntimeError exceptions. For example, re-raise the error if it does not match a predefined message text.

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

Of course, direct text comparison is just an example, you could search for included substrings or regular expressions.

It is worth noting that the .message attribute of exceptions is deprecated starting with Python 2.6. You can find the text in .args, usually args[0].

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

Upvotes: 15

SilentGhost
SilentGhost

Reputation: 319531

try:
    import pyparted
except RuntimeError:
    print('RuntimeError is raised')
    raise

more on exception handling in tutorial.

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

try:
    import pyparted
except RuntimeError as e:
    raise ImportError(e)

Upvotes: 5

Nadia Alramli
Nadia Alramli

Reputation: 114933

try:
   import ...
except RuntimeError:
   # Do something

Upvotes: 1

0x6adb015
0x6adb015

Reputation: 7801

Yes.

   try:
        import module
   except RuntimeError:
         pass

imports are interpreted as any other statement, they are not special. You could do an

if condition:
     import module

Upvotes: 1

Related Questions