By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,833 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,833 IT Pros & Developers. It's quick & easy.

I Need A Placeholder

P: n/a
I'm trying to build a try/except case, and I want to have the except
function like such:

try:
# Do some code here
var = 1 # For example
except:
#Do nothing here

The only problem is if I leave a comment only in the except block, I
get an error back saying that the except block is not properly
formatted, since it has no content other than a comment.

So if anyone could suggest some code to put there as a placeholder
that would be wonderful.
Jun 27 '08 #1
Share this Question
Share on Google+
10 Replies

P: n/a
On Thu, 26 Jun 2008 10:03:47 -0700, Ampedesign wrote:
I'm trying to build a try/except case, and I want to have the except
function like such:

try:
# Do some code here
var = 1 # For example
except:
#Do nothing here

The only problem is if I leave a comment only in the except block, I
get an error back saying that the except block is not properly
formatted, since it has no content other than a comment.

So if anyone could suggest some code to put there as a placeholder
that would be wonderful.
"pass" would work fine.

try:
# Do something that can throw an exception
except:
pass

Jun 27 '08 #2

P: n/a
On Jun 26, 10:06*am, Daniel Mahoney <d...@catfolks.netwrote:
On Thu, 26 Jun 2008 10:03:47 -0700, Ampedesign wrote:
I'm trying to build a try/except case, and I want to have the except
function like such:
try:
* * * # Do some code here
* * * var = 1 * * * * # For example
except:
* * * #Do nothing here
The only problem is if I leave a comment only in the except block, I
get an error back saying that the except block is not properly
formatted, since it has no content other than a comment.
So if anyone could suggest some code to put there as a placeholder
that would be wonderful.

"pass" would work fine.

try:
* * *# Do something that can throw an exception
except:
* * *pass
Thanks. That will work perfectly.
Jun 27 '08 #3

P: n/a
Ampedesign wrote:
I'm trying to build a try/except case, and I want to have the except
function like such:

try:
# Do some code here
var = 1 # For example
except:
#Do nothing here

try:
# Do some code here
var = 1 # For example
except:
pass

Gary Herron

The only problem is if I leave a comment only in the except block, I
get an error back saying that the except block is not properly
formatted, since it has no content other than a comment.

So if anyone could suggest some code to put there as a placeholder
that would be wonderful.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #4

P: n/a
Ampedesign <me**********@gmail.comwrote:
I'm trying to build a try/except case, and I want to have the except
function like such:

try:
# Do some code here
var = 1 # For example
except:
#Do nothing here

The only problem is if I leave a comment only in the except block, I
get an error back saying that the except block is not properly
formatted, since it has no content other than a comment.

So if anyone could suggest some code to put there as a placeholder
that would be wonderful.
Use the 'pass' statement.

But really, you should not have a bare except: if you are going to ignore a
specific exception that's fine, but don't ignore all exceptions. You'll
regret it.
Jun 27 '08 #5

P: n/a
On Jun 26, 10:08*am, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
Ampedesign <metalkeys...@gmail.comwrote:
I'm trying to build a try/except case, and I want to have the except
function like such:
try:
* * * # Do some code here
* * * var = 1 * * * * # For example
except:
* * * #Do nothing here
The only problem is if I leave a comment only in the except block, I
get an error back saying that the except block is not properly
formatted, since it has no content other than a comment.
So if anyone could suggest some code to put there as a placeholder
that would be wonderful.

Use the 'pass' statement.

But really, you should not have a bare except: if you are going to ignorea
specific exception that's fine, but don't ignore all exceptions. You'll
regret it.
This is true. Though I get a wide range of exceptions since I'm
downloading and parsing a file. I will get everything from a socket
error to an empty xml element.

In the future however, I will make separate exceptions.
Jun 27 '08 #6

P: n/a
Ampedesign wrote:
I'm trying to build a try/except case, and I want to have the except
function like such:

try:
# Do some code here
var = 1 # For example
except:
#Do nothing here

The only problem is if I leave a comment only in the except block, I
get an error back saying that the except block is not properly
formatted, since it has no content other than a comment.

So if anyone could suggest some code to put there as a placeholder
that would be wonderful.
except:
pass

is the usual technique there.

j

Jun 27 '08 #7

P: n/a
"Joshua Kugler" <jk*****@bigfoot.comwrote in message
news:ma*************************************@pytho n.org...
except:
pass

is the usual technique there.
Is there any other?
Jun 27 '08 #8

P: n/a
John Salerno wrote:
"Joshua Kugler" <jk*****@bigfoot.comwrote in message
news:ma*************************************@pytho n.org...
>except:
pass

is the usual technique there.

Is there any other?
Sure. Evaluating any side-effect free expression and ignoring the result
will work:

try:
# do somthing
except:
None

try:
# do something
except:
"I don't care about the exception."

Of course, using pass *is* the preferred method.

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 27 '08 #9

P: n/a
John Salerno wrote:
"Joshua Kugler" <jk*****@bigfoot.comwrote in message
news:ma*************************************@pytho n.org...
>except:
pass

is the usual technique there.

Is there any other?
if 0: 42

Proof:
>>def cp(pass_):
.... return compile("try: 1/0\nexcept:\n %s" % pass_, "<nofile>", "exec")
....
>>cp("pass") == cp("if 0: 42")
True

:-)

Peter
Jun 27 '08 #10

P: n/a
"Peter Otten" <__*******@web.dewrote in message
news:g4*************@news.t-online.com...
if 0: 42
How Pythonic. ;-)
Jun 27 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.