C Programming

How to Use Try Catch Statements in C

The words “try” and “catch” describe what to do when a program encounters an exception due to a code or data problem. Exceptions are handled in a try block of code. Catch blocks are utilized to find and handle exceptions from try blocks. Many programming languages, including Python, C++, and JavaScript, include try-catch statements.

This article demonstrates what a try-catch statement is and how it is implemented using an example in C programming.

What is a Try Catch Statement?

The try statement defines a collection of statements that might create an exception. When a specific kind of exception happens, the catch block is where the exception is sent. The compiler will either output an error notice or the exception will continue to travel up the call stack until it is addressed if the try/catch block fails to handle it.

The general syntax of the try-catch statement is given as:

try {
    /*
    Insert some code that will probably generate errors
    */
}
catch {
    /*
    Write a code for handling the generated errors.
    */
}

What is a try-catch Statement in C?

C doesn’t support exception handling and does not have a built-in mechanism to do so. However, you can simulate this to some extent using setjmp and longjmp calls. Without a way to release memory once the stack has been visited, exception-handling mechanisms are inefficient and unsafe, and C does not have a garbage collector. To free up RAM, we would likely also need to integrate context managers.

Now, as we make code improvements, we will gradually construct a solution. The longjmp and setjmp, are two C functions that can be provided by the setjmp.h header file will be utilized by us. The setjmp function accepts jmp_buf type variables and returns 0 when it is called directly. When the same jmp_buf variable is used to invoke longjmp with two variables, the setjmp function returns a value that matches the value of longjmp’s second argument.

An example of the above implementation is given as:

#include <stdio.h>
#include <setjmp.h>
#define TRY do { jmp_buf buf_state; if ( !setjmp(buf_state)) {
#define CATCH } else {
#define ENDTRY }} while(0)
#define THROW longjmp(buf_state, 1)
int main()
{
    TRY {
        printf("Try statement testing\n");
        THROW;
        printf("Statement should not appear, as the THROW block has already thrown the exception \n");
    }
    CATCH {
        printf("Got Exception \n");
    }
    ENDTRY;
    return 0;
}

In the above C program, the ENDTRY function is used for providing the closing part of the do-while block.

Conclusion

If a program encounters an exception due to a data or coding error while it is running, “try” and “catch” describe how to manage it. In a try block of code, exceptions occur while a catch block is where errors from try blocks are found and handled. Many programming languages support the try-catch block but the C does not. This guide described a method to use try-catch statements in C programming.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.