Mainframe programming — NATURAL/ADABAS tutorial. Part 3 — Error Handling and Debugging Techniques

Natalia Nazaruk
6 min readMar 31, 2023

--

Photo by Jeswin Thomas on Unsplash

In the previous articles, we’ve covered the basics of NATURAL programming, from setting up your environment and writing your first “Hello World” program to working with databases using ADABAS. Below you can find all the articles, that I have already written:

  1. Intro — a few words of introduction.
  2. Part 1 — setup process and “Hello World” code
  3. Part 2 — how to work with database
  4. Part 3 — error Handling and Debugging Techniques (the one you are reading now)

As you delve deeper into the world of mainframe programming, you’ll inevitably encounter situations where your code doesn’t work as expected or produces unexpected results. In this article, we will discuss essential error handling and debugging techniques in NATURAL to help you identify, diagnose, and fix issues in your programs.

Table of Contents:

  1. Understanding NATURAL Error Messages
  2. Common NATURAL Errors and Their Causes
  3. Implementing Error Handling in Your Code
  4. Tips and Best Practices for Debugging and Error Handling
  5. Understanding NATURAL Error Messages

Understanding NATURAL Error Messages

When you encounter errors in your NATURAL programs, the system will generate error messages to help you identify the issue. These messages often contain an error code, a brief description of the error, and additional information such as the line number where the error occurred. Understanding how to interpret these error messages is crucial for effective debugging.

Error codes in NATURAL are usually four-digit numbers, with the first digit indicating the error category. For example, a ‘3xxx’ error code typically indicates a syntax error, while a ‘9xxx’ error code usually signifies a runtime error. By familiarizing yourself with the different error categories and their corresponding codes, you’ll be better equipped to diagnose and resolve issues in your NATURAL programs more quickly and efficiently. Consult the NATURAL documentation for a comprehensive list of error codes and their meanings:
https://documentation.softwareag.com/natural/nat638win/print/mc.pdf.

Common NATURAL Errors and Their Causes

There are various types of errors that you may encounter when working with NATURAL. Some of the most common errors include:

  • Syntax errors: These occur when your code contains grammatical mistakes or violates the language’s rules. Examples include mismatched parentheses, misspelled keywords, or incorrect variable declarations.
  • Runtime errors: These errors happen during program execution and can be caused by issues such as division by zero, referencing an uninitialized variable, or exceeding the array bounds.
  • Logic errors: These are harder to identify, as they occur when your code compiles and runs without error messages, but the output is incorrect or unexpected. Logic errors typically stem from incorrect algorithms, flawed assumptions, or misunderstanding the problem you’re trying to solve.

By understanding the different types of errors, you can better determine the root cause of an issue and devise an appropriate solution.

Implementing Error Handling in Your Code

Proactively implementing error handling in your NATURAL programs can help you manage and resolve issues more effectively. By anticipating possible errors and handling them gracefully, you can minimize the impact of unexpected events on your application. Here are some strategies for implementing error handling in your NATURAL code:

  • Use ON ERROR statements: The ON ERROR statement allows you to define custom error handling routines for specific error codes or a range of codes. When an error occurs, NATURAL will execute the corresponding ON ERROR block, allowing you to take appropriate action, such as displaying a custom error message, logging the error, or terminating the program gracefully. For example:
ON ERROR 3001, 3002
WRITE 'A syntax error has occurred. Please check your code.'
ESCAPE BOTTOM
END-ERROR
  • Validate user input: Many runtime errors stem from incorrect or unexpected user input. To prevent these issues, validate user input before using it in your program. For instance, you can check for empty fields, ensure that numeric input is within an acceptable range, or verify that dates are properly formatted.
  • Use the IF statement for error checking: In some cases, you can use the IF statement to check for potential errors before they occur. For example, if you’re dividing by a variable, you can use an IF statement to ensure the variable is not equal to zero before performing the division. This can help prevent runtime errors like division by zero. Here’s an example:
IF divisor NOT = 0
result := dividend / divisor
ELSE
WRITE 'Error: Division by zero is not allowed.'
ESCAPE BOTTOM
END-IF
  • Implement exception handling with subroutines: Another effective way to handle errors in NATURAL is by creating subroutines that handle specific types of exceptions. These subroutines can be called whenever an error occurs or when a particular condition is met. By centralizing your error handling in dedicated subroutines, you can maintain and update your error handling logic more easily.
DEFINE SUBROUTINE HANDLE_ERROR
INPUT errorCode (N4)
INPUT errorMessage (A50)

WRITE 'An error has occurred. Error code:' errorCode
WRITE 'Error message:' errorMessage
ESCAPE BOTTOM
END-DEFINE

IF some_condition
CALL HANDLE_ERROR (9001, 'Custom error message')
END-IF

By incorporating these error handling techniques into your NATURAL programs, you can create more robust and resilient applications that can better cope with unexpected issues and deliver a more consistent user experience.

Tips and Best Practices for Debugging and Error Handling

When working with NATURAL, incorporating efficient debugging and error handling techniques can save you time and effort during the development process. Here are some tips and best practices to help you improve your debugging skills and create more robust NATURAL programs:

  1. Write modular and well-structured code: Breaking your program into smaller, self-contained modules makes it easier to understand, test, and debug. Maintain a clear and consistent structure, and use meaningful variable and subroutine names to enhance readability.
  2. Comment your code: Providing clear and concise comments throughout your code can help you and others understand the purpose of specific sections or algorithms, making it easier to identify potential issues.
  3. Test incrementally: Test your program as you develop, focusing on small units of functionality. This helps to isolate potential issues and makes debugging more manageable.
  4. Use a systematic approach to debugging: When you encounter an issue, try to understand the symptoms and reproduce the error consistently. Then, use a methodical approach to narrow down the possible causes, such as using breakpoints, inspecting variables, or modifying the code to isolate specific sections.
  5. Learn from your mistakes: As you debug your programs, take note of the types of errors you commonly encounter and the techniques you use to resolve them. Over time, you’ll become more proficient at identifying and fixing

It’s worth to note, that those rules apply to all of the programming languages.

Understanding NATURAL Error Messages

In this article, we have discussed various strategies and best practices for debugging and error handling in NATURAL programming. To recap, here are the key points covered:

  • Understanding NATURAL Error Messages: Familiarize yourself with the different error categories and their corresponding codes to diagnose and resolve issues more efficiently. Consult the NATURAL documentation for a comprehensive list of error codes and their meanings.
  • Implementing Error Handling: Use techniques such as ON ERROR statements, input validation, IF statements for error checking, and exception handling with subroutines to create more robust and resilient applications.
  • Best Practices for Debugging and Error Handling: Write modular and well-structured code, comment your code, test incrementally, use a systematic approach to debugging, and learn from your mistakes to improve your debugging skills and create more reliable applications and programs.

By following these guidelines and leveraging the available tools, you’ll be better equipped to identify, diagnose, and resolve issues in your NATURAL programs effectively. This, in turn, will help you create more reliable and robust applications, ensuring a smoother development process and a better overall user experience.

If you like this article and would like to see more of this in the future — feel free to follow my profile to be up-to-date!

This article is part of a series I write about NATURAL programming. If you see any errors in my articles, please let me know! If you like the article and you believe there should be more articles about Mainframes please clap a bit, leave a comment or share this link wherever you want.

--

--

Natalia Nazaruk

Android developer (Kotlin 💯). #GDG3city organiser 💻 Soft Skills advocate. Winner of Google & Udacity scholarships. Dogs & Cats 😍