Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly ignore exceptions

When you just want to do a try-except without handling the exception, how do you do it in Python?

Is the following the right way to do it?

try:     shutil.rmtree(path) except:     pass 
like image 936
Joan Venge Avatar asked Apr 08 '09 16:04

Joan Venge


People also ask

How do I ignore an exception?

To ignore an exception in Java, you need to add the try... catch block to the code that can throw an exception, but you don't need to write anything inside the catch block.

How do you handle specific exceptions?

The order of catch statements is important. Put catch blocks targeted to specific exceptions before a general exception catch block or the compiler might issue an error. The proper catch block is determined by matching the type of the exception to the name of the exception specified in the catch block.

Should I always handle exceptions?

It depends on what you need. If you need to handle different types of exceptions in different ways then you should use multiple catch blocks and catch as much specific exceptions as you can. But sometimes you may need to handle all exceptions in the same way. In such cases catch(Exception) may be ok.


2 Answers

try:     doSomething() except:      pass 

or

try:     doSomething() except Exception:      pass 

The difference is that the first one will also catch KeyboardInterrupt, SystemExit and stuff like that, which are derived directly from exceptions.BaseException, not exceptions.Exception.

See documentation for details:

  • try statement
  • exceptions
like image 145
vartec Avatar answered Nov 10 '22 00:11

vartec


It's generally considered best-practice to only catch the errors you are interested in. In the case of shutil.rmtree it's probably OSError:

>>> shutil.rmtree("/fake/dir") Traceback (most recent call last):     [...] OSError: [Errno 2] No such file or directory: '/fake/dir' 

If you want to silently ignore that error, you would do:

try:     shutil.rmtree(path) except OSError:     pass 

Why? Say you (somehow) accidently pass the function an integer instead of a string, like:

shutil.rmtree(2) 

It will give the error "TypeError: coercing to Unicode: need string or buffer, int found" - you probably don't want to ignore that, which can be difficult to debug.

If you definitely want to ignore all errors, catch Exception rather than a bare except: statement. Again, why?

Not specifying an exception catches every exception, including the SystemExit exception which for example sys.exit() uses:

>>> try: ...     sys.exit(1) ... except: ...     pass ...  >>> 

Compare this to the following, which correctly exits:

>>> try: ...     sys.exit(1) ... except Exception: ...     pass ...  shell:~$  

If you want to write ever better behaving code, the OSError exception can represent various errors, but in the example above we only want to ignore Errno 2, so we could be even more specific:

import errno  try:     shutil.rmtree(path) except OSError as e:     if e.errno != errno.ENOENT:         # ignore "No such file or directory", but re-raise other errors         raise 
like image 37
dbr Avatar answered Nov 10 '22 00:11

dbr