Hide SQL Errors with Try Catch Blocks

If you are working on any PeopleCode that involves inserting or updating data in a table, and you want to avoid showing any SQL errors to the user, then use a try-catch section.

For example, I was working on some code to log extra information about a transaction to a logging table. In the event that the logging failed due to a SQL error, the entire transaction would stop. Not a good thing to happen to a user who doesn't know or care about the fact that you are doing some system logging.

Here's my basic code wrapped in a try-catch section:

try
  SQLExec(SQL.EXAMPLE, &p1, &p2, &p3);
catch Exception &ex
  /* Ignore the exception and move on */
end-try;

In the above, you could have some code to deal with the exception. But the point in my case was to not display a fatal SQL error which stopped the transaction from continuing. This is really just for safety. I'm not expecting a SQL error but if one does ever happen, the code will move on and not abort the transaction.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License