throw exception

Write your question here.

Function()
{

if else
{
}
else
{
}
if( THIS OCCURS){
#if exception
throw : std::bad_alloc();
#else
std: cout << "
#endif
}
CODE HERE <----- Will this code run
}


Will CODE HERE run if the throw is called? Will it run if the std out occurs?
Last edited on
No, you need to use a try-catch block to catch the exception.
So when #if exception it hit it will end after the throw or std:: cout ?
1
2
3
4
5
6
7
8
try
{
  //Code here
}
catch (const std::bad_alloc &e)
{
  //Code here
}
If you throw an exception, the remaining code in the function won't execute.

If you cout a statement then, of course, the code will continue to execute - why wouldn't it?

Edit: And please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Btw, there is no catch block in your code which is also expected to be embedded in a try block so the program will probably crash and that's if u don't hit a compile-time error.

on the one hand, if u have the appropriate catch block, flow control will move over to the catch block and the remainder of the code will be useless.

on the other "leg", if no error is thrown then the code continues unhindered.

#TRYTHROWCATCH #EXCEPTION
Topic archived. No new replies allowed.