what is try catch code example
Example 1: java try catch
try {
} catch(ErrorName e){
}
Example 2: try block in java
try {
}
catch(Exception e) {
}
Example 3: try catch error
#include <iostream>
using namespace std;
template <class T>
void getMin(T val1, T val2)
{
try
{
if (val1 < val2)
cout << val1 << " is the minimum\n";
else if (val1 > val2)
cout << val2 << " is the minimum\n";
else
throw 505;
}
catch(T my_ERROR_NUM)
{
cout << "Input is invalid, try again. ";
}
}
template <class T>
void getMax(T val1, T val2)
{
try
{
if (val1 > val2)
cout << val1 << " is the maximum\n\n";
else if (val1 < val2)
cout << val2 << " is the maximum\n\n";
else
throw 505;
}
catch (T random_num)
{
cout << "Error 505!\n\n";
}
}
Example 4: try/catch
var x = 7, y = 7;try { if(x == y){ throw "errorOne"; }else{ throw "errorTwo"; }} catch(e) { if(e == "errorOne"){ console.log("Same")} if(e == "errorTwo") console.log("Different)}
Example 5: try catch exception
First try block try to handle it
if not then catch block will handle it.
Finally block will executed regardless
of the outcome