Anil Kumar
Anil Kumar

Reputation: 493

a small issue in using exception handling in c++

The below given simple exception program is giving Unhandled Exception "Microsoft C++ exception: int at memory location 0x0012fe94..". I am getting this error immediately after the function excep() is returned. Please can anyone tell why this error is coming here. Also it will be helpful if all the possible mistakes in this code were explained/analysed. I am learning to code better.

I am using Visual C++ 2005.

#include <iostream>
using namespace std;

int main ()
{
   int excep(int);

   throw excep(20);

   return 0;
}

int excep(int e)
{

    cout << "An exception occurred. Exception Nr. " << e << endl;
    return 0;

}

Upvotes: 2

Views: 1475

Answers (3)

Sebastian Mach
Sebastian Mach

Reputation: 39089

In the line

throw excep(20);

excep(20) is called first, whose return value is then thrown, i.e. you throw an integer. It is roughly equivalent to:

const int i = excep(20);
throw i;

To get you an idea how exception-snytax looks:

#include <iostream>
#include <stdexcept>
using namespace std;

int main ()
{
   try {
       // do something
       throw std::runtime_error("test");
   } catch (const std::exception &e) {
       std::cout << "exception: " << e.what() << std::endl;
   }
}

In practive: NEVER throw anything that is not derived from std::exception.

Proposed Readings:

Upvotes: 1

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247909

What did you expect to happen?

You call a function which prints out "An exception occurred", plus the other text, and then you throw the resulting value as an exception, which you never catch, so the program reports than an uncaught exception was thrown. Which is true, because that's exactly what you just did.

The entire point in exceptions is that when you throw an exception, it will propagate out through the program, until it is either handled, or it reaches the top level and terminates the program. You don't handle the exception, so it terminates the program.

If you want to handle an exception, you need to wrap the throwing code in a try block, followed by a catch, as in:

try {
    throw excep(20);
}
catch (int ex) {
   // do something about the exception
}

Upvotes: 3

zabulus
zabulus

Reputation: 2513

If you try to learn exception throwing/handling your code contains an error. function excep must handle object that was thrown not to be thrown itself. your code must be rewritten as follows:

using namespace std;
int excep(int e) 
{ 
       cout << "An exception occurred. Exception Nr. " << e << endl; 
       return 0;
}
int main () 
{ 
     int excep(int);
     try
     { // <--------- Begin of try block. required part of the exception handling
         throw 20;
     }
     catch (const int &errCode) // <------- we are catching only ints
     {
             excep(errCode); // Function that handles exception
     }

     return 0; 
}  

It is good design not to throw int variables, but type that inherites std::exception. But this is more advanced exception using knowledge

Upvotes: 3

Related Questions