Answers for "catch (Exception)"

7

try catch java

public class MyClass {
  public static void main(String[ ] args) {
    try {
      int[] myNumbers = {1, 2, 3, 4, 5, 6};
      System.out.println(myNumbers[10]);
    } catch (Exception e) {
      System.out.println("Something went wrong. check again");
    }
  }
}
Posted by: Guest on July-17-2020
6

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
Posted by: Guest on January-05-2021
0

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\n";
        else if (val1 > val2)
            cout << val2 << " is the minimum\n";
        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\n\n";
        else if (val1 < val2)
            cout << val2 << " is the maximum\n\n";
        else
            throw 505; // exception error processing when input is invalid
    }
    catch (T random_num)
    {
        cout << "Error 505!\n\n"; // Second part of error messagee
    }
}
Posted by: Guest on November-26-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language