Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 8 ■ EXCEPTIONS 165 Note that the parentheses around the exceptions in the except clause are important; a common error is to omit them, in which case you may end up with something other than what you want. For an explanation, see the next section, “Catching the Object.” Catching the Object If you want access to the exception itself in an except clause, you can use two arguments instead of one. (Note that even when you are catching multiple exceptions, you are only supplying except with one argument—a tuple.) This can be useful (for example) if you want your program to keep running, but you want to log the error somehow (perhaps just printing it out to the user). The following is an example program that prints out the exception (if it occurs), but keeps running: try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except (ZeroDivisionError, TypeError), e: print e The except clause in this little program again catches two types of exceptions, but because you also explicitly catch the object itself, you can print it out so the user can see what happened. (You see a more useful application of this later in this chapter, in the section “When All Is Well.”) A Real Catchall Even if the program handles several types of exceptions, some may still slip through. For example, using the same division program, simply try to press Enter at the prompt, without writing anything. You should get a stack trace somewhat like this: Traceback (most recent call last): File 'exceptions.py', line 3, in ? x = input('Enter the first number: ') File '', line 0 ^ SyntaxError: unexpected EOF while parsing This exception got through the try/except statement—and rightly so. You hadn’t foreseen that this could happen, and weren’t prepared for it. In these cases it is better that the program crash immediately (so you can see what’s wrong) than that it simply hide the exception with a try/except statement that isn’t meant to catch it. However, if you do want to catch all exceptions in a piece of code, you can simply omit the exception class from the except clause:
166 CHAPTER 8 ■ EXCEPTIONS try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except: print 'Something wrong happened...' Now you can do practically whatever you want: Enter the first number: "This" is *completely* illegal 123 Something wrong happened... ■Caution Catching all exceptions like this is risky business because it will hide errors you haven’t thought of as well as those you’re prepared for. It will also trap attempts by the user to terminate execution by Ctrl-C, attempts by functions you call to terminate by sys.exit, and so on. In most cases, it would be better to use except Exception, e and perhaps do some checking on the exception object, e. When All Is Well In some cases, it can be useful to have a block of code that is executed unless something bad happens; as with conditionals and loops, you can add an else clause: try: print 'A simple task' except: print 'What? Something went wrong?' else: print 'Ah...It went as planned.' If you run this, you get the following output: A simple task Ah...It went as planned. With this else clause, you can implement the loop hinted at in the section “Catching Two Exceptions with One Block,” earlier in this chapter: while 1: try: x = input('Enter the first number: ') y = input('Enter the second number: ') value = x/y print 'x/y is', value except: print 'Invalid input. Please try again.' else: break
- Page 145 and 146: 114 CHAPTER 6 ■ ABSTRACTION Can I
- Page 147 and 148: 116 CHAPTER 6 ■ ABSTRACTION stora
- Page 149 and 150: 118 CHAPTER 6 ■ ABSTRACTION 4. Yo
- Page 151 and 152: 120 CHAPTER 6 ■ ABSTRACTION The p
- Page 153 and 154: 122 CHAPTER 6 ■ ABSTRACTION >>> p
- Page 155 and 156: 124 CHAPTER 6 ■ ABSTRACTION Also,
- Page 157 and 158: 126 CHAPTER 6 ■ ABSTRACTION Feel
- Page 159 and 160: 128 CHAPTER 6 ■ ABSTRACTION ■No
- Page 161 and 162: 130 CHAPTER 6 ■ ABSTRACTION So yo
- Page 163 and 164: 132 CHAPTER 6 ■ ABSTRACTION numer
- Page 165 and 166: 134 CHAPTER 6 ■ ABSTRACTION LAMBD
- Page 167 and 168: 136 CHAPTER 6 ■ ABSTRACTION In th
- Page 169 and 170: 138 CHAPTER 6 ■ ABSTRACTION Scope
- Page 171 and 172: 140 CHAPTER 7 ■ MORE ABSTRACTION
- Page 173 and 174: 142 CHAPTER 7 ■ MORE ABSTRACTION
- Page 175 and 176: 144 CHAPTER 7 ■ MORE ABSTRACTION
- Page 177 and 178: 146 CHAPTER 7 ■ MORE ABSTRACTION
- Page 179 and 180: 148 CHAPTER 7 ■ MORE ABSTRACTION
- Page 181 and 182: 150 CHAPTER 7 ■ MORE ABSTRACTION
- Page 183 and 184: 152 CHAPTER 7 ■ MORE ABSTRACTION
- Page 185 and 186: 154 CHAPTER 7 ■ MORE ABSTRACTION
- Page 187 and 188: 156 CHAPTER 7 ■ MORE ABSTRACTION
- Page 189 and 190: 158 CHAPTER 7 ■ MORE ABSTRACTION
- Page 191 and 192: 160 CHAPTER 8 ■ EXCEPTIONS >>> im
- Page 193 and 194: 162 CHAPTER 8 ■ EXCEPTIONS Custom
- Page 195: 164 CHAPTER 8 ■ EXCEPTIONS More T
- Page 199 and 200: 168 CHAPTER 8 ■ EXCEPTIONS Invali
- Page 201 and 202: 170 CHAPTER 8 ■ EXCEPTIONS If you
- Page 204 and 205: CHAPTER 9 ■ ■ ■ Magic Methods
- Page 206 and 207: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 208 and 209: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 210 and 211: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 212 and 213: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 214 and 215: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 216 and 217: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 218 and 219: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 220 and 221: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 222 and 223: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 224 and 225: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 226 and 227: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 228 and 229: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 230 and 231: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 232 and 233: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 234 and 235: CHAPTER 10 ■ ■ ■ Batteries In
- Page 236 and 237: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 238 and 239: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 240 and 241: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 242 and 243: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 244 and 245: CHAPTER 10 ■ BATTERIES INCLUDED 2
166 CHAPTER 8 ■ EXCEPTIONS<br />
try:<br />
x = input('Enter the first number: ')<br />
y = input('Enter the second number: ')<br />
print x/y<br />
except:<br />
print 'Something wrong happened...'<br />
Now you can do practically whatever you want:<br />
Enter the first number: "This" is *completely* illegal 123<br />
Something wrong happened...<br />
■Caution Catching all exceptions like this is risky business because it will hide errors you haven’t thought<br />
of as well as those you’re prepared for. It will also trap attempts by the user <strong>to</strong> terminate execution by Ctrl-C,<br />
attempts by functions you call <strong>to</strong> terminate by sys.exit, and so on. In most cases, it would be better <strong>to</strong> use<br />
except Exception, e and perhaps do some checking on the exception object, e.<br />
When All Is Well<br />
In some cases, it can be useful <strong>to</strong> have a block of code that is executed unless something bad<br />
happens; as with conditionals and loops, you can add an else clause:<br />
try:<br />
print 'A simple task'<br />
except:<br />
print 'What? Something went wrong?'<br />
else:<br />
print 'Ah...It went as planned.'<br />
If you run this, you get the following output:<br />
A simple task<br />
Ah...It went as planned.<br />
With this else clause, you can implement the loop hinted at in the section “Catching Two<br />
Exceptions with One Block,” earlier in this chapter:<br />
while 1:<br />
try:<br />
x = input('Enter the first number: ')<br />
y = input('Enter the second number: ')<br />
value = x/y<br />
print 'x/y is', value<br />
except:<br />
print 'Invalid input. Please try again.'<br />
else:<br />
break