good evening.
Code:#include <iostream> using std::cout; using std::cin; using std::endl; #include <exception> using std::exception; int main() { int val1, val2, result; val1 = val2 = result = 0; cout << "Please enter two values separated by spaces:\n"; while(cin >> val1 >> val2) { try { result = val1 / val2; cout << "Result: " << result << endl; } catch(exception err) { cout << "Error: Second value must be higher than 0\n"; cout << err.what() << "\nTry again? Enter y or n" << endl; char c; cin >> c; if(!cin || c == 'n') break; } } return 0; }Code:thames@semaht ~/C++/Exceptions $ g++ -g -Wall exc2.cc -std=c++11 thames@semaht ~/C++/Exceptions $ valgrind ./a.out ==4606== Memcheck, a memory error detector ==4606== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==4606== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==4606== Command: ./a.out ==4606== Please enter two values separated by spaces: 5 0 ==4606== ==4606== Process terminating with default action of signal 8 (SIGFPE) ==4606== Integer divide by zero at address 0x402CB32CD ==4606== at 0x400E07: main (exc2.cc:17) ==4606== ==4606== HEAP SUMMARY: ==4606== in use at exit: 0 bytes in 0 blocks ==4606== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==4606== ==4606== All heap blocks were freed -- no leaks are possible ==4606== ==4606== For counts of detected and suppressed errors, rerun with: -v ==4606== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) Exceção de ponto flutuante



8Likes
LinkBack URL
About LinkBacks



