Answers for "try catch c++ example"

C++
4

C++ try catch

try {
   //do something
} catch (const std::exception& e) {
     std::cout << e.what(); // information from error printed
}
Posted by: Guest on April-23-2021
1

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;
}
Posted by: Guest on October-28-2021
6

c++ try

try {
	//do
} catch (...){
	//if error do
}
Posted by: Guest on March-02-2020
2

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;
}
Posted by: Guest on July-13-2020

Browse Popular Code Answers by Language