c ++ excepción lanzamiento desnudo [duplicado]
Frecuentes
Visto 416 veces
0
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?
2 Respuestas
6
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
1
#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 c++ windows visual-c++ exception try-catch or haz tu propia pregunta.
I'm not going to use the throw information though. Is there some standard practice for that maybe like
throw std::defaultthrow
? - loopsUsted 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 likethrow false;
, or use the standardstd::runtime_error()
. - diez cuatro