Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception handling(restricting exceptions)

Tags:

c++

exception

i was dealing with the following code,& got confused,please have a look at it

#include<iostream>
#include<conio.h>
#include<string.h>

using namespace std;

void Xhandler(int test) throw(char,double)  /*i am restricting an integer exception     here by not including it in the argument list of throw*/
{
if(test==0) throw test;
if(test==1) throw 'a';
if(test==2) throw 123.23;
}

int main()
{
cout<"start";

try{
    Xhandler(0);  
}

catch(int i)  /*this catch statement must be ignored then,but it is running*/
{
    cout<<"caught an integer"; /*this is the output on the screen*/
}
catch(char c)
{
    cout<<"caught character";
}
catch(double a)
{
    cout<<"caught double";
}

cout<<"end";
    _getch();
    return 0;

     }

the catch statement corresponding to int must be ignored(which is not ignored) & program must have to be terminated as there is no matching catch statement left,but it is not so?

like image 290
nobalG Avatar asked Feb 22 '26 10:02

nobalG


2 Answers

You did not specified the compiler. If you are using Microsoft visual C++ there is a note at Exception Specifications page on the MSDN

Visual C++ departs from the ANSI Standard in its implementation of exception specifications. The following table summarizes the Visual C++ implementation of exception specifications:

...

throw(type) - The function can throw an exception of type type. However, in Visual C++ .NET, this is interpreted as throw(...).

When using g++ the process is terminated due unexpected exception when running your example.

like image 64
Michy Avatar answered Feb 25 '26 00:02

Michy


Well, the reason the

catch(int i)  /*this catch statement must be ignored then,but it is running*/
{
    cout<<"caught an integer"; /*this is the output on the screen*/
}

exception handler gets executed instead of the unexpected handler being called lies probably with your compiler.

The standard conform behavior when an exception is thrown by a function with exception specification but the type of the thrown exception does not match the exception specification would be that the unexpected handler gets called. The default handler terminates the program.

In reality many compilers ignore exception specifications and some might just issue some warnings telling you that. That the compilers ignore them is a good idea imho.

An exception specification in C++ does not work the way you would like it to. An exception specification does not mean that the compiler guarantees that the function cannot throw any other exceptions besides the ones mentioned in the exception specification. In a standard conform compiler it just means that the compiler has to enforce the exception specification at runtime (if the exception being thrown matches the exception specification you are fine, if not you should end up in the unexpected handler)

For more info on exception specifications and why you should avoid them see here (Herb Sutter explains there mostly that exception specifications in C++ are not what they should be/most people think they are). And you get some further links at the bottom of the page...

like image 29
ds27680 Avatar answered Feb 24 '26 23:02

ds27680



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!