I Need A Placeholder

A

Ampedesign

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.
 
D

Daniel Mahoney

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
 
G

Gary Herron

Ampedesign said:
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
 
A

Ampedesign

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.

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.
 
J

Joshua Kugler

Ampedesign said:
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
 
C

Carsten Haese

John said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top