◐ Shell
clean mode source ↗

exception specification - cppreference.com

De cppreference.com

<metanoindex/>

Lista as exceções que uma função pode, direta ou indiretamente jogar.

Original:

Lists the exceptions that a function might directly or indirectly throw.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Sintaxe

throw(typeid, typeid, ...) (obsoleta)

Explicação

Se uma função é declarada com T tipo listado em sua especificação de exceção, a função pode lançar exceções desse tipo ou um tipo derivado dele.

Original:

If a function is declared with type T listed in its exception specification, the function may throw exceptions of that type or a type derived from it.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Se a função lança uma exceção do tipo não listada em sua especificação de exceção, o std::unexpected função é chamada. A função padrão chama std::terminate, mas pode ser substituído por uma função fornecida pelo usuário (via std::set_unexpected) que pode chamar std::terminate ou lançar uma exceção. Se a exceção lançada de std::unexpected é aceito pela especificação de exceção, pilha desenrolar continua como de costume. Se não for, mas std::bad_exception é permitido pela especificação excepção, std::bad_exception é lançada. Caso contrário, é chamado std::terminate.

Original:

If the function throws an exception of the type not listed in its exception specification, the function std::unexpected is called. The default function calls std::terminate, but it may be replaced by a user-provided function (via std::set_unexpected) which may call std::terminate or throw an exception. If the exception thrown from std::unexpected is accepted by the exception specification, stack unwinding continues as usual. If it isn't, but std::bad_exception is allowed by the exception specification, std::bad_exception is thrown. Otherwise, std::terminate is called.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Exemplo

class X {};
class Y {};
class Z : public X {};
class W {};

void f() throw(X, Y) 
{
    int n = 0;
    if (n) throw X(); // OK
    if (n) throw Z(); // also OK
    throw W(); // will call std::unexpected()
}

Veja também