Page 431 Friday, December 14, 2001 2:04 PM in Microsoft Office Development qr-codes in Microsoft Office Page 431 Friday, December 14, 2001 2:04 PM

How to generate, print barcode using .NET, Java sdk library control with example project source code free download: Page 431 Friday, December 14, 2001 2:04 PM using barcode printing for microsoft office control to generate, create qr-codes image in microsoft office applications. ISO Specification 12 . Exception Handling from the base-class exce QR Code 2d barcode for None ption. Therefore, the body of the class contains only the keyword pass the keyword that indicates a suite or block performs no work. The remainder of the program (lines 10 37) demonstrates our programmer-defined exception class.

The program enables the user to input a numeric value, then invokes function squareRoot (lines 10 18) to calculate the square root of that value. For this purpose, squareRoot invokes function math.sqrt, which wants a nonnegative value as its argument.

If math.sqrt receives a negative value, the function raises a ValueError exception with the argument "math domain error". In this program, we essentially write our own square root function that uses a programmer-defined exception to prevent the user from calculating the square root of a negative number.

If the numeric value received from the user is negative, function squareRoot raises a NegativeNumberError (lines 14 16). Otherwise, squareRoot invokes function math.sqrt to compute the square root.

In the main program, a while loop (lines 20 37) continues executing until the user enters a nonnegative value. The try suite (lines 24 25) attempts to obtain a numerical value from the user and to pass that value to function squareRoot. When the user inputs a value and presses Enter, the program passes the user-entered value to function float.

If the value is not a number, function float raises a ValueError exception, and the except handler in lines 28 29 prints an error message. Control then returns to the beginning of the while loop. If the user inputs a negative number, function squareRoot raises a NegativeNumberError.

The except handler in lines 32 33 simply prints the exception object before control returns to the beginning of the while loop. If the user enters a valid, nonnegative number, line 25 prints the square root of the number before program control proceeds to the else clause in lines 36 37. The else suite contains only the keyword break, which terminates the while loop.

In this chapter, we demonstrated how the exception-handling mechanism works and discussed how to make applications more robust by writing exception handlers to process potential problems. When developing new applications, it is important to investigate potential exceptions raised by the functions your program invokes or by the interpreter, then implement appropriate exception-handling code to make those applications more robust. In 13, String Manipulation and Regular Expressions, we begin a discussing a series of techniques for developing substantial software.

These techniques, when combined with disciplined exception handling, enable Python programmers to create viable, valuable software components.. SUMMARY An exception is an ind QR Code ISO/IEC18004 for None ication of a special event that occurs during a program s execution. Often the special event is an error (e.g.

, dividing by zero or adding two incompatible types). Sometimes the special event is something else (e.g.

, the termination of a for loop). Exception handling enables programmers to write clear, robust, more fault-tolerant programs that can resolve (or handle) exceptions. The style and details of exception handling in Python are based on the Modula-3 language.

This exception-handling mechanism is similar to that used in C# and Java. The raise statement executes to indicate that an exception has occurred. This is called raising (or sometimes throwing) an exception.

The simplest raise statement consists of the keyword raise, followed by the name of the exception to be raised.. Page 432 Friday, December 14, 2001 2:04 PM Exception Handling 12 . Exception names specif Microsoft Office qrcode y classes and Python exceptions are objects of those classes. When the raise statement executes, Python creates an object of the specified exception class. The raise statement may specify an argument or arguments that initialize the exception object.

In this case, a comma follows the exception name, and the argument or a tuple of arguments follows the comma. Exception handling enables the programmer to remove error-handling code from the main line of the program s execution. This improves program clarity and enhances modifiability.

Programmers can decide to handle whatever exceptions they choose all types of exceptions, all exceptions of a certain type or all exceptions of related types. The exception-handling mechanism is useful for processing problems that occur when a program interacts with reusable software components. Rather than internally handling problems that occur, such components use exceptions to notify client code of problems.

This enables programmers to implement error handling that is appropriate to each application. Exception handling is geared to situations in which the code that detects an error is unable to handle it. Such code raises or throws an exception.

Python uses try statements to enable exception handling. The try statement encloses statements that potentially cause exceptions. A try statement consists of keyword try, followed by a colon (:), followed by a suite of code in which exceptions may occur, followed by one or more clauses.

Immediately following the try suite may be one or more except clauses (also called except handlers). Each except clause specifies zero or more exception names that represent the type(s) of exceptions the except clause can handle. The except clause also may specify an identifier for the exception that was raised, and the handler can use the exception object to obtain information about that exception.

An except clause that specifies no exception type is an empty except clause, which catches all exception types. It is a syntax error to place an empty except clause before any other except clauses in a particular try statement. After the last except clause, an optional else clause contains code that executes if the code in the try suite raised no exceptions.

A try suite can be followed by zero except clauses; in that case, it must be followed by a finally clause. The code in the finally suite always executes, regardless of whether an exception occurs. Programmers sometimes refer to the point in the program at which an exception occurs as the throw point.

Exceptions are objects of classes that inherit from class Exception. If an exception occurs in a try suite, the try suite expires and program control transfers to the first matching except handler (if there is one) following the try suite. A match occurs if the types are identical or if the raised exception s type is a derived class of the handler s exception type.

If no exceptions occur in a try suite, the interpreter ignores the exception handlers for that try statement. If an exception occurs in a statement that is not in a try suite and that statement is in a function, the function containing that statement terminates immediately and the interpreter attempts to locate an enclosing try statement in a calling function a process called stack unwinding. Python is said to use the termination model of exception handling, because the try statement enclosing a raised exception expires immediately when that exception occurs.

Function float raises a ValueError exception if the function cannot convert its argument value to a floating-point value..
Copyright © . All rights reserved.