0

How to catch error inside "else" which that "else" inside "try". Here is the code:

try:
 if appcodex == app:
 print "AppCode Confirmed"
 if acccodex == acc:
 print "Access Code Confirmed"
 if cmdcodex == cmd:
 print "Command Code Confirmed"
 print "All Code Confirmed, Accessing URL..."
 else:
 print "Command Code not found"
 else:
 print "Access Code not found"
 else:
 print "AppCode not found"
except:
 print "Error : Code doesn't match..."

How to raise "CommandCode not found" instead of "Error : Code doesn't match..." when cmdcodex/cmd has no input.

Burhan Khalid
175k20 gold badges255 silver badges292 bronze badges
asked Jul 16, 2014 at 6:20
8
  • 1
    Which part of that do you expect will cause an error, exactly? You have equality comparisons and prints, there's not much to go wrong. Don't get in the habit of wrapping everything in try and certainly avoid bare except (see e.g. this). What is it that you are trying to do? Commented Jul 16, 2014 at 6:45
  • when there is no cmdcodex/cmd, i want it raising "Command code not found". but it give me output "Error : Code doesn't match" and thanks for link, i'll read it :D Commented Jul 16, 2014 at 6:54
  • What do you mean "no cmdcodex/cmd"? Will they be assigned to None, or "", or unassigned? What error are you guarding against? Commented Jul 16, 2014 at 6:55
  • the data was in database, i fetch it and store it in cmdcodex then i selecting the word then i store the selected word in cmd. there is any probability that cmdcodex will not same as cmd. so, when it happen I want raising "Command code not found" :D Commented Jul 16, 2014 at 6:59
  • But that's simply being unequal, which isn't an error. I am trying to understand why you have wrapped the whole thing in try. Commented Jul 16, 2014 at 7:01

2 Answers 2

3

You'll need to create your own exception and raise it. Its as simple as creating a class that inherits from Exception, then using raise:

>>> class CommandCode(Exception):
... pass
...
>>> raise CommandCode('Not found')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
__main__.CommandCode: Not found
answered Jul 16, 2014 at 6:29
Sign up to request clarification or add additional context in comments.

Comments

1

It is normal you get "Error : Code doesn't match..." instead of "Command Code not found". Why ? The answer is basic: you need to understand the basic concepts of handling exceptions in Python.

In your special case, you must wrap that piece of code within a try .. except block also, like this:

try:
 if appcodex == app:
 print "AppCode Confirmed"
 if acccodex == acc:
 print "Access Code Confirmed"
 try:
 if cmdcodex == cmd:
 print "Command Code Confirmed"
 print "All Code Confirmed, Accessing URL..."
 except:
 print "Command Code not found"
 else:
 print "Access Code not found"
 else:
 print "AppCode not found"
except:
 print "Error : Code doesn't match..."

To sum up the situation: you can nest as necessary try ... except blocks as you need. But you should follow this PEP

Josh Crozier
242k56 gold badges401 silver badges316 bronze badges
answered Jul 16, 2014 at 6:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.