Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What throw statement throws if i do not tell what type of object is to be thrown in c++?

The following code terminates abnormally as no object is explicitly thrown. What is thrown by throw statement in the following code?

int main()
{
  try{
  cout<<"try";
  throw ;
}
catch(...){
 cout<<"catch";
 }
return 0;
}
like image 741
Farsan Rashid Avatar asked Jul 13 '15 06:07

Farsan Rashid


People also ask

Which type of object is thrown by a throw statement?

The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class.

What does throw () mean in C?

throw () is an exception specifier that declares that what() will never throw an exception. This is deprecated in C++11, however (see http://en.wikipedia.org/wiki/C++11). To specify that a function does not throw any exception, the noexcept keyword exists in C++11.

What does the throw statement do?

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

How do you throw an exception object in C++?

An exception in C++ is thrown by using the throw keyword from inside the try block. The throw keyword allows the programmer to define custom exceptions. Exception handlers in C++ are declared with the catch keyword, which is placed immediately after the try block.


2 Answers

throw without an argument should only be used inside a catch statement, to rethrow the caught exception object. You code tries to use it outside the catch statement - instead you should pick a type to throw, if in doubt it's not unreasonable to start with std::runtime_error. For more options, see here. You can also throw your own types, but it's usually a good idea to derive them from one of the Standard-library provided types so client code has a better chance at specifying appropriate handling for all logically similar errors, rather than having to catch and handle them separately and being constantly updated for each new possible error.

FWIW, the Standard says in 15.1/9:

If no exception is presently being handled, executing a throw-expression with no operand calls std::terminate().

So very explicitly, the answer to "What is thrown..." is that no throwing is done, and std::terminate is called instead.

like image 171
Tony Delroy Avatar answered Oct 19 '22 04:10

Tony Delroy


So the question is: "What happens when I throw outside a catch block?" The answer to this can be found in its documentation:

Rethrows the currently handled exception. Abandons the execution of the current catch block and passes control to the next matching exception handler (but not to another catch clause after the same try block: its compound-statement is considered to have been 'exited'), reusing the existing exception object: no new objects are made. This form is only allowed when an exception is presently being handled (it calls std::terminate if used otherwise). The catch clause associated with a function-try-block must exit via rethrowing if used on a constructor.

Emphasize mine.

like image 27
Baum mit Augen Avatar answered Oct 19 '22 05:10

Baum mit Augen