throw exception

Sep 7, 2016 at 2:01am
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 Sep 7, 2016 at 2:01am
Sep 7, 2016 at 6:52am
No, you need to use a try-catch block to catch the exception.
Sep 7, 2016 at 11:08am
So when #if exception it hit it will end after the throw or std:: cout ?
Sep 7, 2016 at 12:21pm
1
2
3
4
5
6
7
8
try
{
  //Code here
}
catch (const std::bad_alloc &e)
{
  //Code here
}
Sep 7, 2016 at 5:34pm
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 Sep 7, 2016 at 5:35pm
Sep 7, 2016 at 7:57pm
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.