Skip to main content

Posts

Showing posts with the label SEH

Recover from exception and re-execute the code.

 In this post, I am trying to show how can we catch an exception, then correct the cause of the exception and re-execute the code. In Windows, we have something called SEH (Structured Exception Handling) using which we can have a chance to recover from exceptions and re-execute the code which originally raised an exception. So what I am trying to do here in my sample code is: 1. Accepting two numbers from the user and deliberately taking the denominator as 0; 2. Trying to divide by numerator with zero. 3. Division with zero results as an exception. 4. Catching the exception and asking the user to re-enter the denominator with a non-zero value and then executing the division part of the code again.  Simple! This same thing can be done for other type of exceptions too. Let's see how it works in the below demo code! #include <windows.h> #include <iostream> class ADemoToRecover { public:         int numerator = 0;         int denominator = 0; public:     int recoverFromDivi