Exception Handling

Use the Exception class to do exception handling in your PeopleCode. This class provides a try, catch, and throw mechanism so you don't need to check after each operation for errors. Instead, by the structure of the try-catch blocks, you can declare when you are interested in handling exceptions, and how you want to handle them.

Exceptions are instances or subclasses of the PeopleCode Exception class. Oracle recommends that when applicable, application class methods should throw exceptions instead of communicating back to the calling code with return values. You can create exceptions by:

  • Creating an Exception base class that encapsulates the built-in function call and handles its function parameters consistently. This is the preferred way.

  • Calling the built-in function CreateException.

Since a method can throw exceptions for several different reasons, Oracle recommends that you:

  • Create Exception subclasses often.

  • Throw strongly typed exceptions.

This allows the calling code to make decisions based on the exception type.

For example, a method that retrieves customer data from a database might throw an exception named SQLException if a database error occurs, or an exception named PrivilegeException if the current user doesn’t have the appropriate permissions to perform the operation. There could be different errors causing either of these exception types to be thrown in this method; if necessary, you can write the calling code to examine the specific message numbers and react accordingly.

Oracle recommends that you always catch an exception; if it is not caught, it causes a runtime error. Use a try-catch block.

Not catching an exception causes a runtime error and can cause PeopleTools to display a dialog box with the message catalog entry and information about the technical context of the exception. The reasons for avoiding this by using try-catch blocks are:

  • Application code often continues some form of processing even in the event of an exception; you should not assume that the script stops all processing if an exception occurs.

  • The error message that PeopleTools displays might not be appropriate for end-users because of its technical information.