barcode generator in c# windows application codeproject Catching Two Exceptions with One Block in Font

Creator PDF 417 in Font Catching Two Exceptions with One Block

Catching Two Exceptions with One Block
Print PDF417 In None
Using Barcode maker for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
Barcode Creator In None
Using Barcode encoder for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
If you want to catch more than one exception type with one block, you can specify them all in a tuple, as follows: try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except (ZeroDivisionError, TypeError): print 'Your numbers were bogus...' In the preceding code, if the user either enters a string or something other than a number, or if the second number is zero, the same error message is printed. Simply printing an error message isn t very helpful, of course. An alternative could be to keep asking for numbers until the division works. I show you how to do that in the section When All Is Well, later in this chapter.
PDF417 Generator In None
Using Barcode generator for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
UPC - 13 Generation In None
Using Barcode maker for Font Control to generate, create EAN / UCC - 13 image in Font applications.
www.OnBarcode.com
CHAPTER 8 EXCEPTIONS
Barcode Drawer In None
Using Barcode maker for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Create Quick Response Code In None
Using Barcode creator for Font Control to generate, create QR image in Font applications.
www.OnBarcode.com
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.
Print GTIN - 128 In None
Using Barcode printer for Font Control to generate, create GS1 128 image in Font applications.
www.OnBarcode.com
Drawing British Royal Mail 4-State Customer Code In None
Using Barcode encoder for Font Control to generate, create British Royal Mail 4-State Customer Code image in Font applications.
www.OnBarcode.com
Catching the Object
PDF417 Generation In Java
Using Barcode generator for BIRT reports Control to generate, create PDF 417 image in BIRT applications.
www.OnBarcode.com
PDF 417 Creation In None
Using Barcode generator for Online Control to generate, create PDF-417 2d barcode image in Online applications.
www.OnBarcode.com
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. )
Barcode Drawer In Java
Using Barcode creation for BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
Draw Data Matrix ECC200 In None
Using Barcode drawer for Excel Control to generate, create ECC200 image in Microsoft Excel applications.
www.OnBarcode.com
A Real Catchall
1D Barcode Creation In Java
Using Barcode drawer for Java Control to generate, create Linear image in Java applications.
www.OnBarcode.com
Data Matrix Drawer In Objective-C
Using Barcode maker for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
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 '<string>', 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:
Printing Barcode In None
Using Barcode generation for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Barcode Generator In Java
Using Barcode creation for BIRT Control to generate, create Barcode image in Eclipse BIRT applications.
www.OnBarcode.com
CHAPTER 8 EXCEPTIONS
Generating ANSI/AIM Code 39 In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create Code 39 Full ASCII image in ASP.NET applications.
www.OnBarcode.com
Scanning Code 128C In .NET Framework
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
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...
Making PDF417 In Java
Using Barcode printer for Eclipse BIRT Control to generate, create PDF417 image in BIRT applications.
www.OnBarcode.com
EAN-13 Scanner In .NET Framework
Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
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
Copyright © OnBarcode.com . All rights reserved.