DEV Community

Peter + AI
Peter + AI

Posted on

πŸš€ Mastering Exception Handling in Uniface: The `catch` Statement Explained

Exception handling is a crucial aspect of robust software development, and Uniface provides powerful tools to manage errors gracefully. Today, we'll dive deep into the catch statement and how it works within Uniface's try...endtry blocks. πŸ›‘οΈ

πŸ” What is the catch Statement?

The catch statement in Uniface is used within try...endtry blocks to handle exceptions that might occur during ProcScript execution. It's your safety net for graceful error handling! 🎯

πŸ“ Basic Syntax

try
    <ProcScript that may throw an exception>
{catch} {ErrorNumber1 {,ErrorNumberN}}
    <Procscript that handles the error>
endtry
Enter fullscreen mode Exit fullscreen mode

πŸŽ›οΈ Parameters

Parameter Data Type Description
ErrorNumbers Numeric A comma-separated list of error numbers that the catch statement will catch. If omitted, the catch block catches any exception.

πŸ”§ Key Features and Rules

πŸ’‘ Multiple catch Blocks

You can have multiple catch statements for a single try block to handle different types of errors:

  • Each catch block handles only the exceptions specified in its comma-separated list
  • Multiple catch blocks are processed in order
  • The endtry command must come after the last catch statement

🎯 Catch-All Handler

If you specify a catch statement without arguments, it catches any exception, but it must be the last catch block!

πŸ“Š Error Information Access

Within a catch block, you have access to valuable error information through:

  • $procerror - Contains the error details
  • $procerrorcontext - Provides context about where the error occurred

⚠️ Important: This error information is only available within the specific catch block!

🌟 Practical Example

Here's a real-world example showing how to use the catch statement:

trigger detail
 try
 call Do_It()
 catch
 putmess "An error occurred: %%($procerror): %%($procerrorcontext)" 
 endtry
end
Enter fullscreen mode Exit fullscreen mode

This code calls the Do_It() function and displays a user-friendly error message if an exception occurs. πŸ“’

πŸ”„ Advanced Features

Rethrow Statement

A catch block can contain a rethrow statement, allowing you to rethrow an exception that was caught. This is useful when you want to:

  • Log the error locally
  • Perform cleanup operations
  • Pass the exception to a higher level for further handling

Exception Bubbling

πŸ”₯ A catch block can also throw an exception itself! When this happens, the exception bubbles up to a higher level in your application.

⚠️ Restrictions to Remember

Keep these limitations in mind when working with catch blocks:

  • goto statements are not allowed inside catch blocks
  • ProcScript labels are not allowed inside catch blocks
  • Both will result in compilation errors! 🚫

πŸŽ‰ Conclusion

The catch statement is an essential tool for building robust Uniface applications. It allows you to handle errors gracefully, provide meaningful feedback to users, and maintain application stability even when things go wrong. 🌈

Remember to:

  • βœ… Use specific error numbers when possible
  • βœ… Keep catch-all handlers as the last catch block
  • βœ… Utilize $procerror and $procerrorcontext for debugging
  • βœ… Consider using rethrow for multi-level error handling

Happy coding, and may your exceptions be always caught! 🎯✨


πŸ“š This article is based on the official Uniface 10.4 documentation and was created with AI assistance to help developers better understand exception handling in Uniface.

Top comments (0)