try_catch

Nov 18, 2014 at 11:45pm
try {

//statements
//no throw statement for bad_alloc

}
catch (type var){

//statements
//catch bad_alloc
}

VS

ptr_var=new (nothrow) [size]

whats the difference and why even if bad_alloc not defined in iostream catch can catch bad_alloc error?
Nov 19, 2014 at 1:46am
nothrow version of new does not throw bad_alloc. Instead, it will return a NULL (nullptr?) pointer. This is useful for when you don't care to use exceptions at all.

As for the second portion of the question, you'll have to provide more context for the given problem.
Nov 19, 2014 at 6:32am
ok then how come catch handles error even though throw is not used in try statement?
Nov 19, 2014 at 6:41pm
You will have to provide more context.
Nov 19, 2014 at 7:28pm
You mean that you (unsuccessfully) allocate memory with new or malloc and get try to be executed?
Nov 25, 2014 at 3:09pm
yee
Nov 25, 2014 at 3:14pm
What header you need for bad_alloc:

http://www.cplusplus.com/reference/new/bad_alloc/?kw=bad_alloc

catch handles something when it's thrown. Otherwise it does nothing
Topic archived. No new replies allowed.