Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does: throw 0 do/mean? Is it "bad"?

Tags:

c++

exception

Context

I came across some code, like this:

if( Some_Condition ) throw 0;

I googled a bit, and found a few other code snippets using that odd looking throw 0 form.

I presume one would catch this as:

catch(const int& e) 
{  }

Or is this a NULL ptr? to be caught as void* ?

Question

What does this throw 0 do? Is it special in some way?

My normal preference would be to throw something that is (or derived from) std::exception. So to me this looks "bad". Is it "bad" ?

like image 884
MartinP Avatar asked Apr 13 '10 10:04

MartinP


2 Answers

Generally throw can throw any type, any you need to catch it with this type or its base type.

So technically it is legal code but...

it is bad code: You should always derive your exceptions from std::exception or at least from some class that provides some useful information about error rather then plain number. But deriving from std::exception is the correct way because it allows to use topmost catch(std::exception const &e) and get at least some information about the error.

like image 118
Artyom Avatar answered Oct 15 '22 02:10

Artyom


It is not special, you can thow int just as an exception class.

It is considered a poor style, because an exception class can tell more about what actually happened.

like image 36
Pavel Radzivilovsky Avatar answered Oct 15 '22 04:10

Pavel Radzivilovsky