Using goto for Exception Handling in C
Exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. C doesn’t provide any specialized keywords for this purpose but in C++ to handle exceptions we have to try, throw and catch keywords.
try -> Represents a block of code that can throw an exception. catch -> Represents a block of code that is executed when a particular exception is thrown. throw -> Used to throw an exception. Also used to list the exceptions that a function throws but doesn’t handle itself.
In C programming language we use the goto keyword for the same purpose. With the use of goto, we implement the throw keyword. The goto statement can be used to jump from anywhere to anywhere within a function.
Why Exception Handling?
Sometimes a block of code results in an error that will terminate a program. To overcome this problem, we will use the goto keyword which will make the program execution jump out of the code that is causing program termination. The output of the following program explains the flow of execution of exception and its handling using goto.
Before try Exception Caught After catch (Will be executed)
Please Login to comment...