Code-level Exception Handling

Code-level Exception handling allows you to write code to deal with problems that occur within the scope of a PBL task.

PBL uses the following block structure to handle exceptions:
do
on Exception
on Exit
end

The do-end block, though not required, allows you to clearly define the scope of an exception within your code. This is particularly important if you need to nest exceptions within multiple do/end blocks.

The on Exception statement allows you to define and execute the exception code.

The on Exit statement allows you to perform any clean up that is required as a result of the exception. This can include cleaning up temporary systems files or database table created within your PBL code.

Exception Handling Example.

The following code example demonstrates the syntax for using the on Exception and on Exit constructs.

on Objects.TooCloseToDueDate do
sendAlert project
    using mailSubject = project.name + "is getting too close to due date", 
          mailMessage = "The project " + project.name + "is getting to cloose to due date."

end

on Objects.Overdue do
sendAlert project
    using mailSubject = project.name + "is overdue", 
          mailMessage = "The project " + project.name + "is overdue."

end

on exit do
sendAlert project
    using mailSubject = "Work on project " +project.name + " was started.", 
          mailMessage = "Work on  project " + project.name + " was started on " + project.startDate 
          + ". The project leader for is: " + project.projectLeaderId

end
          

This example uses the default PBL programming style. Other programming styles use different keywords for exception handling, but the underlying concept is the same. If you are using the Java programming style, this is implemented within a try-catch block.

Propagating Exceptions to the Process Level

In many cases, you may not want to catch code exceptions. Within your process it may make more sense to let them propagate up to process-level exceptions. Any exceptions you choose to explicitly handle within a PBL program must be able to be resolved within the code. If an exception cannot be completely resolved it should propagate up as a process-level exception.

Handling Exceptions Directly in a Method

You can also handle exceptions directly in a method. See Compound Statements for more information. can also be directly included in an onException. The do/end block is implicit/ contains the do/end block.

This also allows you to narrow the scope of exception catching.