Function Call based exception handling

A
ll the methods so far have one inherent limitation, the error message generated can be determined at compile time. Some times you may want to give more information like

 
You gave me -100 for a course number

Another possible situation: An error has occurred. But some special cases does not require user intervention, the script should be able to take care of it.

In either of these situations you need a function call based exception handling. In this case the exception is raised using

raise WebError ("bad_crse_num()",self.args)

When the exception handler sees a "()" at the end of the first argument, it assumes that you want to handle the exception in your own function. The name of the function is handle_bad_crse_num(self,page,message), i.e. function name is got by prepending the first argument with "handle_". The function should take three arguments: self (since it is a method of the Display class), page and message. The page argument contains the page object which contains the error page. The message argument is whatever was passed in the second argument in your raise statement. In this case it is self.args. Since arguments in python can be any type, you can send as much information as you want to your exception handler, by properly packing it and unpacking it.

Caveat:

After your function has finished it is assumed that the page object has the contents of the error page to be displayed. So it is your responsibility to populate the page object. The simplest way, is to call generate_error_page with appropriate arguments. If you are trying to rectify some mistake, it might easier to bounce the user to the same URL with the correct arguments, than to call make_page from within your exception handling function. Of course, make sure that there are no infinite bounce loops.