1

If I just want to throw a string, isn't there a built in type somewhere so that I can just do

throw standard_exception("This is wrong!");

Or do I have to define such a standard exception that derives from exception myself? I know it is very simple to do so, I just thought this would be so common that it would be defined somewhere.

Thanks

Cookie
  • 12,004
  • 13
  • 54
  • 83

4 Answers4

6

std::runtime_error and std::logic_error (both derived from std::exception) both have constructors that take strings and override the what() member function to return the provided string.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • It is interesting that every link I found tells one not to throw strings, but to define your own custom exception. The option to throw a runtime_error or the like never gets mentioned. – Cookie Jul 22 '11 at 17:43
  • I prefer to define my own exceptions derived from `runtime_error` for individual components; doing so makes correcter exception handling quite a bit easier because you can handle specific errors and let other errors be handled elsewhere. I do use `logic_error` directly, though, but only for errors that really are logic errors (e.g., the code is wrong), sort of like an assertion. – James McNellis Jul 22 '11 at 17:45
  • True, but if all one wants to do is for the thread to fall over without affecting the rest of the application, anything that gets caught by exception works. I am not doubting that more advanced throwing and catching warrants your own exception, but for a quick "this is impossible, now die" I think this is certainly more suited. I feel that in my situation defining your own exception as recommended on other posts on here or the C++ FAQ is overkill and for newbies not the most lightweight introduction to exceptions. – Cookie Jul 22 '11 at 17:55
  • @Cookie: During development just to make things are working I use `throw "This is wrong"` or `throw int(4)`. But this is for development only. Once I have worked out how everything should be I put correct exception objects in to the code. PS. I don't recommend doing this unless you have an automated way of detecting and removing them (I have a script on the build server to make sure I can't check these in) – Martin York Jul 22 '11 at 18:01
6

If you want to throw a string, you can do so by just writing

throw "I'm throwing a string!";

This isn't a particularly good idea, though, since it's not considered good form to throw things like char*s as exceptions. If you want to wrap the string into an exception of some form, you can always just use runtime_error or logic_error:

throw logic_error("This is wrong!");
throw runtime_error("This is wrong!");
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
4

Runtime error should be what you are looking for

throw runtime_error("This is wrong");
Soren
  • 14,402
  • 4
  • 41
  • 67
1

You can throw std::runtime_error or create your own class that inherits from std::exception as follows

#include <exception>
#include <string>


class myexcept : public std::exception
{
private:
  /**
   * Reason for the exception being thrown
   */
  std::string what_;

public:
  /**
   * Class constructor
   *
   * @param[in] what
   *    Reason for the exception being thrown
   */
  myexcept( const std::string& what ) : what_( what ){};


  /**
   * Get the reason for the exception being thrown
   *
   * @return Pointer to a string containing the reason for the exception being thrown
   */
  virtual const char* what() const throw() { return what_.c_str(); }


  /**
   * Destructor 
   */
  virtual ~myexcept() throw() {}
};

To throw

throw myexcept( "The reason goes here" );
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • 1
    [The destructor must have an exception specification.](http://stackoverflow.com/questions/3233078/how-does-an-exception-specification-affect-virtual-destructor-overriding) – James McNellis Jul 22 '11 at 17:39