/* Palavras reservadas 'try', 'catch','throw'. *Try: Tentar executar um bloco de codigo; *Catch: Pegar algo para tratar; *Trow: Lança uma exceção(erro) que a funcão pode pegar através do 'catch'. */ #include <iostream> using namespace std; double div(double n1, double n2) { if (n2 == 0) throw "Divisao por zero!!\n"; return n1 / n2; } int fat(int n) // Fatorial. { if (n < 0) throw "Numero negativo!!!"; if (n == 0 || n == 1) return 1; // Porque fatorial de zero ou um é um. return n * fat(n - 1); } int main(int argc, char *argv[]) { try { cout << "Fatorial de 5: " << fat(5) << endl; // Tento executar esses blocos. } catch (const char *e) // Se ocorrer algum erro, será capturado pelos 'Catchs'. { // Exibe a frase com erro. cerr << "Erro: " << e << endl; } catch (...) // Você pode ter vários 'Catchs' pra capturar a exceção. { // Pega qualquer outra coisa. cerr << "Erro inesperado!" << endl; } return 0; }
I propose with this blog to share my doubts and knowledge about my private learning in the powerful and fascinating C ++ language, adjacencies and curiosities researched by me in technology. I believe that this will help me one day to write computational logic poems comprehensible to low- and medium-level language, making me, perhaps, a new possibility.
segunda-feira, 5 de março de 2018
Palavras reservadas: Try, Catch, Throw !
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário