C++ try catch
try {
//do something
} catch (const std::exception& e) {
std::cout << e.what(); // information from error printed
}
C++ try catch
try {
//do something
} catch (const std::exception& e) {
std::cout << e.what(); // information from error printed
}
try catch c++ example
//https://youtu.be/EyXXLpFriMc has a great explaination
#include<iostream>
using namespace std;
int main() {
int numerator, denominator, result;
cin >> numerator >> denominator;
try {
if (denominator == 0){
throw denominator; //throw jumps code to the catch block so that division will never happen.
}
result = numerator/denominator;
} catch (int receivedDenominator) {
result = 0;
}
cout << result;
return 0;
}
exception handling c++
// exceptions
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us