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 thecatch
statement and how it works within Uniface'stry...endtry
blocks. 🛡️
🔍 What is the catch Statement?
Thecatch
statement in Uniface is used withintry...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
🎛️ 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 multiplecatch
statements for a singletry
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 acatch
statement without arguments, it catches any exception, but itmust 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)" endtryend
This code calls theDo_It()
function and displays a user-friendly error message if an exception occurs. 📢
🔄 Advanced Features
Rethrow Statement
A catch block can contain arethrow
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
Thecatch
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)
For further actions, you may consider blocking this person and/orreporting abuse