Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

Thanks in advance.
I am trying to restrict exception object data-types from in one of my function. But not able to do it.
As per my understanding, below code should not throw "char" type exception. But still my program is unable to restrict "char" type throw?
I am using visual studio 2008.

Please see the code below:
void FuncThrow(int choice)throw(int, float)
{
switch(choice)
{
case 1: throw 10;
break;
case 2: throw 10.0f;
break;
case 3: throw 'x';
break;
default:
break;
}
}
int main()
{
try
{
FuncThrow(3);
}

catch(int)
{
cout << "Exception from int"<<endl;
}
catch(float)
{
cout << "Exception from float"<<endl;
}
catch(char)
{
cout << "Exception from character"<<endl;
}


}
Posted
Updated 14-Jul-11 3:51am
v2
Comments
Richard MacCutchan 14-Jul-11 10:59am    
What do you mean by per my understanding, below code should not throw "char" type exception., when that is exactly what your code is doing?

Microsoft's compiler doesn't support exception specification except throw().

Source
 
Share this answer
 
Comments
ggupta2009 14-Jul-11 10:50am    
Thank you very much...
can you please suggest which would be better alternate to do this?
lewax00 14-Jul-11 11:45am    
You can still add them, just make sure you follow them yourself I guess. Or use a different compiler that supports them. I haven't really used them much myself (because I use Visual Studio mostly so there's no point).
Philippe Mori 16-Jul-11 18:04pm    
Exception specifications are not really helpful (except maybe an empty throw specification) so the best suggestion is to not uses them.

If you really want, you can simulate the effect by writting a try/catch block and handling exception in a similar way that would happen on a conforming compiler.
Richard MacCutchan 14-Jul-11 10:58am    
This is not correct, you can throw any type.
lewax00 14-Jul-11 11:45am    
Yes you can throw any type, but Microsoft's compiler does not allow you to restrict which types can be thrown, with the exception of throwing nothing.
The restriction of the type to be thrown would abuse the structural exception handling. There are no situations where is can be helpful.

—SA
 
Share this answer
 
Comments
Philippe Mori 16-Jul-11 17:16pm    
Probably one of the biggest mistake in C++ standard was the exception specification... Complicate for almost no benefits and a few problem like what to do with virtual function... Generally, the base class should not know what exception a derived class would throw.

Another problem is when the code need to be redesigned and a new exception must be thrown, all calling functions must be modified if they themselves have a specification that do not include the new exception. It might even be worst when an application is composed of multiple dynamic libraries (DLL).

In pratice, the only usefull thing to know is that a function would not throw...
Sergey Alexandrovich Kryukov 17-Jul-11 13:09pm    
Thank you, Philippe. I agree with your notes.
--SA
See http://msdn.microsoft.com/en-us/library/wfa0edys.aspx[^]

for more information on Visual C++ implementation.
 
Share this answer
 
If you want to simulate exception specification, you can write something like:
void myfunction() throw(int)
try
{
   // Code here
}
catch(int)    // Repeat for each exception in specification
{
    throw;
}
catch(...)
{
    try
    {
        unexpected();
    }
    catch(int)  // Repeat for each exception in specification
    {
    }
    catch(...)
    {
        if (/* std::bad_exception in throw specification */)
            throw std::bad_exception();
        else
            terminate();
    }
}


This would be very similar to what would happen on a conforming compiler...

You can see that for specification with multiple types, the code can rapidly become complex. A compiler might implement that in a more optimal fashion but it does give an idea of what happen under the hood.
 
Share this answer
 
Comments
Philippe Mori 16-Jul-11 18:31pm    
Here I have used a function-try-block. I don't know if it was supported in VS 2008. If not, then you can simply add a pair of { } around the external try-catch block.
An interesting artice on exception specification (why they are evil)

http://www.linuxprogrammingblog.com/cpp-exception-specifications-are-evil[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900