Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

try catch error

// Main program passes in two ints, checks for errors / invalid input
// using template class type T for all variables within functions
#include <iostream>
using namespace std;

template <class T> // make function return type template (T)
void getMin(T val1, T val2)
{
    try
    {
        if (val1 < val2) // if val1 less than return it as min
            cout << val1 << " is the minimum
";
        else if (val1 > val2)
            cout << val2 << " is the minimum
";
        else 
            throw 505; // exception error processing when input is invalid
    }
    catch(T my_ERROR_NUM)
    {
        cout << "Input is invalid, try again. "; // first part of error message
    }
}

template <class T>
void getMax(T val1, T val2) // make function return type template (T)
{
    try
    {
        if (val1 > val2) // if val1 greater then return it as max
            cout << val1 << " is the maximum

";
        else if (val1 < val2)
            cout << val2 << " is the maximum

";
        else
            throw 505; // exception error processing when input is invalid
    }
    catch (T random_num)
    {
        cout << "Error 505!

"; // Second part of error messagee
    }
}
Source by reflectoring.io #
 
PREVIOUS NEXT
Tagged: #catch #error
ADD COMMENT
Topic
Name
8+7 =