Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an empty indentation block in Python?

The runtime keeps telling me:

expected an indented block

But I don't want write nothing inside my except block, I just want it to catch and swallow the exception.

like image 801
Jader Dias Avatar asked Oct 07 '09 00:10

Jader Dias


People also ask

How do you create an indent block in Python?

To indicate a block of code in Python, you must indent each line of the block by the same amount. The two blocks of code in our example if-statement are both indented four spaces, which is a typical amount of indentation for Python.

What's an indented block in Python?

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.

Which symbol is used to represent an indented block?

Python uses the colon symbol (:) and indentation for showing where blocks of code begin and end. Indentation makes the code more readable and in python, indentation is very important.


1 Answers

Just write

pass 

as in

try:     # Do something illegal.     ... except:     # Pretend nothing happened.     pass 

EDIT: @swillden brings up a good point, viz., this is a terrible idea in general. You should, at the least, say

except TypeError, DivideByZeroError: 

or whatever kinds of errors you want to handle. Otherwise you can mask bigger problems.

like image 170
Peter Avatar answered Oct 20 '22 14:10

Peter