c ++ excepción lanzamiento desnudo [duplicado]

Posible duplicado:
¿Cuál es la diferencia entre lanzar y lanzar con el argumento de excepción detectada?
Atrapa (…) trabaja en el lanzamiento; sin objeto?

Esto se bloqueará:

try
{
    if(1)
        throw;
}
catch(...)
{
    printf("hi");
}

I thought I could do that but I guess not. What is the right way to throw when you don't need any information?

preguntado el 26 de agosto de 12 a las 19:08

2 Respuestas

A "naked throw" re-throws an exception that has already been caught. Doesn't work well if there is nothing to rethrow.

You can really throw anything, like throw "Error!";, even if that is not too useful. You could otherwise try

if (x == 1)
    throw std::runtime_error("x == 1 is not a good value here")`.

Respondido 26 ago 12, 20:08

I'm not going to use the throw information though. Is there some standard practice for that maybe like throw std::defaultthrow? - loops

Usted debe use the exception though. catch(...) is basically worthless as you will have no idea how to recover from "any" exception. If you insist on a completely generic exception type though, you have the option of simple types like throw false;, or use the standard std::runtime_error(). - diez cuatro

#include <exception>

try
{
    if(1)
        throw std::exception();
}
catch(...)
{
    printf("hi");
}

This might be better, depending on what you are up to:

class my_exception : public std::exception {};

entonces,

try
{
    if(1)
        throw my_exception();
}
catch(my_exception)
{
    printf("hi");
}

Respondido 26 ago 12, 23:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.