try {} catch (err) { } code example
Example 1: try catch throwing error in javascript
let json = '{ "age": 30 }';
try {
let user = JSON.parse(json);
alert( user.name );
} catch (e) {
alert( "doesn't execute" );
}
Example 2: 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";
}
}