Sign in to follow this  
y2jsave

why exception not caught in this case!!

Recommended Posts

#include<iostream>
using namespace std;

void fun()
{
throw 4;
}

int main()
{
set_new_handler(fun);

try
{
int* hh = new int[100000000000000];
}
catch(bad_alloc)
{
cout<<"caught bad alloc";
}
catch(int)
{
cout<<"caughy a int";
}
catch(...)
{
cout<<"caugth something";
}

return 0;
}



output
terminate called after throwing an instance of 'int'
Abort


thanks in advance ..


Share this post


Link to post
Share on other sites

Try catching std::unexpected&.

The signature of standards ::operator new() is
void *operator new(std::size_t sz) throw(std::bad_alloc)

If you try throwing anything else but the documented exceptions, it is unexpected. Read up on exception specifications.

Share this post


Link to post
Share on other sites
The new handler function must either make more memory available or throw bad_alloc or a class derived from bad_alloc. Anything else is undefined behavior.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
If you try throwing anything else but the documented exceptions, it is unexpected. Read up on exception specifications.

No, std::unexpected can only be thrown if the exception specification includes std::unexpected. If it doesn't and the unexpected handler still throws std::unexpected, terminate() is called. Read up on exception specifications.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
No, std::unexpected can only be thrown if the exception specification includes std::unexpected. If it doesn't and the unexpected handler still throws std::unexpected, terminate() is called. Read up on exception specifications.

Um, std::unexpected() is a function. My bad. The exception class I was thinking of is std::bad_exception.

The default action of std::unexpected() is to throw std::bad_exception. If no catch handler for std::bad_exception then std::terminate() is called. See [15.5.2](3).

Either way, this is still the cause of the OP's observed behaviour. He needs to refer to [18.6.1.2] regarding ::operator new[](), [15.4] regarding exception specifications, and [18.8.2.1] regarding violating exception specifications.

This behaviour is extremely well defined.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
No, he violates the requirements of 18.4.2.2, which means that the behavior is undefined.

I see that requirement as [18.6.2.3](2). Which version of the standard are you quoting?

The behaviour of a new_handler that does not exhibit the required behaviour of [18.6.2.3](2) is undefined by [1.3.13] because the standard does not specifiy the result of a user-defined function new_handler function. The standard is very clear as to the behaviour of an exception not present in an exception specification list (see previous references) and such operation is well defined.

It's a matter of language lawyer debate, but the implementations I am familiar with invoke well-defined behaviour over undefined behaviour where there is a conflict.

I believe the violation of exception specification clauses [15.4] and [18.8.2] explain the observed behaviour of the OP's problem and the violation of [18.6.2.3](2) is a red herring.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
Quote:
Original post by SiCrane
No, he violates the requirements of 18.4.2.2, which means that the behavior is undefined.

I see that requirement as [18.6.2.3](2). Which version of the standard are you quoting?

ISO/IEC 14882:2003.

Quote:
It's a matter of language lawyer debate, but the implementations I am familiar with invoke well-defined behaviour over undefined behaviour where there is a conflict.

You've forgotten the first rule of undefined behavior: undefined behavior includes behaving exactly how you would expect based on other language features. Once you invoke undefined behavior, anything can happen. You have no guarantees one way or the other.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this