• 0

[Python] Handling exceptions


Question

Hello all,

I need some help with a portion of my program I am currently writing. I've been just recently learning Python (2.6) and basically I want my code to check for any exceptions that may have been raised and deal with them. Now I have created some custom exceptions of my own so what I want is for the program to print a certain thing when it sees those exceptions raised, print another thing when it sees ANY other exceptions and continue when no exceptions are raised at all. I am sort of stuck.....What I have so far is this:

try:

'''some code'''

except CustomException1:

return 'blah'

except CustomException2:

return 'meh'

except CustomException3:

return 'wha'

else:

return 'nah'

where else deals with NO raised exceptions. Any ideas?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Hello all,

I need some help with a portion of my program I am currently writing. I've been just recently learning Python (2.6) and basically I want my code to check for any exceptions that may have been raised and deal with them. Now I have created some custom exceptions of my own so what I want is for the program to print a certain thing when it sees those exceptions raised, print another thing when it sees ANY other exceptions and continue when no exceptions are raised at all. I am sort of stuck.....What I have so far is this:

try:

'''some code'''

except CustomException1:

return 'blah'

except CustomException2:

return 'meh'

except CustomException3:

return 'wha'

else:

return 'nah'

where else deals with NO raised exceptions. Any ideas?

try:


except:

code

i will catch any errors

Link to comment
Share on other sites

  • 0

Im not sure i understand what you want completely, but how about this?

import sys

try:

something

except Custom1:

something

sys.exit(0)

..

except:

Any other exception goes here

sys.exit(0)

down here, we made it through

Link to comment
Share on other sites

  • 0

You seem to be right on the money with the code you got there. I don't understand what you don't understand.

#check for any exceptions that may have been raised
try:
    #mycode
#print a certain thing when it sees those exceptions raised
except MyCustomException:
    #print a certain thing
except MyOtherCustomException:
    #print a certain thing
else:
    # the rest of the code goes here so it will only execute if no exception happened

Link to comment
Share on other sites

  • 0

Ah, I think he's stuck trying to print.

One way to handle the problem is just:

resultingString = None
try:
    doSomeStuff()
except:
    resultingString = "Oh my God, an Exception!"
else:
    pass #This means: do nothing, it'll keep resultingString as None

if resultingString != None:
    print resultingString

This allows you to do extra things with the resulting string afterwards.

Another option is, like Dr_Asik posted, to simply place 'print' in the location where you handle the exceptions.

But Dr_Asik is right, it's quite unclear what the problem is exactly.

Link to comment
Share on other sites

  • 0

I was able to figure out using nested try: statements haha....Thank you to everyone who posted here, you've all helped. I also got some great ideas!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.