# Markus Heinonen, fall 2009 # Task 3 (7p): Explain the most important exception handling mechanism in python. ============ Model answer ============ Exception handling is a computer language paradigm where exceptional or erroneous situations are handled using special notation, semantics or an idiom. In python, the intended mechanism to handle exceptions is to raise and catch instances of Exception-classes using try-except syntax. The exception handling separates the code producing and handling the error. Often the local level producing the error doesn't know how to handle the error, while the caller-side most likely is able to handle the situation appropriately. Whenever any function or a code block notices a situation in which an error should be signaled and the normal code execution cannot proceed, it can raise an exception. The syntax for raising is: raise exception-instance raise exception-class[(params)] raise # no specifier, re-raises exception in except-block The exception is a class derived form ExceptionBase-class or one of its subclasses. Python provides a set of predefined exceptions, such as ValueError, DivideByZeroError, etc. organized into an hierarchy of different kinds of exceptions. The exception class often contains information or an error message. Raising an exception terminates the execution of the code block or function and throws the exception to the calling code block. It can either catch and suppress the exception, or let it fall to the preceding calling functions or modules. If an exception is not caught, the program is terminated when the exception falls to the "__main__"-module level, and the traceback is printed. The syntax for exception handling is: try: # code which might result in an exception raised except Exception [, exc-obj-reference]: # code to fix the problem, notify user, etc. except OtherException: # ... else: (optional) # go here if no exceptions catched finally: (optional) # always go here The advantage of the exception mechanism is that the program doesn't necessarily crash when an error occurs. Exceptions give a chance to fix the problem or at least log the situation and terminate the program gracefully. Also, exceptions adhere to the structured programming paradigm and provide semantic hints to inform what kind of exceptions can happen in e.g. a function. This forces the programmer to think of all the errors that can happen and provide error handling. A disadvantage of exceptions is that the code easily becomes littered with try-except blocks. Exceptions also reveal parts of the implementation of some module or package, and when the module adds a new exception, the caller code has to be changed to accomodate this. It is easy to circumvent the exceptions by always catching just exception base classes. When catching too general exception, details of more specific exceptions might be lost. Exceptions break the normal execution flow of the program and thus it might make it harder to understand the code. In C the dominant mechanism is to have functions return error codes (or 0 if no problems occur) and then to check the value of the error code and act appropriately. This is mostly made possible by the fact that in C the result of the function can be placed on one of the parameters. Other often used convention is to check the input values of a function before the function call. ================== grading guidelines ================== 1p for general knowledge of the exception mechanism 1p for general understanding of the exception classes, instances and hierarchy 1p for general understanding of the syntax (try-except-else-finally) 1p for general understanding of raising exceptions (raise) 1p for proper discussion of advantages (crash-prevention, problem-fixing, separation of problem and handling, etc.) 1p for proper discussion of disadvantages (code-verbosity, circumventing exceptions is easy, too-general-exception, brokes normal flow, etc.) 1p for understanding of at least one alternative way to handle exceptions (return values, error codes, etc) Each score can be given as half if the understanding or explanation is moderately lacking or only slightly erroneous. In addition, the final score can be modified [-2..+1] based on literary merits and essay-likeness (-2 if no proper sentences, -1 if no proper paragraphs, +1 if fluent and well-thought answer). The final score is rounded up.