Try Catch
Int Min Value
1.) First method: Try catch has NumberFormatException with numberFormatExceptMinValue
2.) Second method: If else statement from Min_Value to Max_Value of Int with errorCorrectionInt
Int Max Value
1.) First method: Try catch has NumberFormatException with numberFormatExceptMaxValue
2.) Second method: If else statement from Min_Value to Max_Value of Int with errorCorrectionInt
Long Min Value
1.) Try catch has NumberFormatException with numberFormatExceptMaxValue
ArrayIndexOOB
uses the exception ArrayIndexOutOfBounds
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println();
// Print the message if exception occurred
// First method: Int Min Value using try catch
//Number is smaller than Min_Value of Int -2147483648
System.out.println("Int smallest number is: " + Integer.MIN_VALUE);
numberFormatExceptMinValue("-2147483649");
numberFormatExceptMinValue("10");
System.out.println();
// Second method: Int Min Value using Long to compare Min value
System.out.println("Int smallest number is: " + Integer.MIN_VALUE);
errorCorrectInt("-2147483649");
errorCorrectInt("10");
System.out.println();
// First method: Int Max Value using try catch
// Number is larger than Max_Value of Int 2147483647
System.out.println("Int largest number is: " + Integer.MAX_VALUE);
numberFormatExceptMaxValue("2147483648");
numberFormatExceptMaxValue("10");
System.out.println();
// Second method: Int Max Value using Long to compare Min value
// Number is larger than Max_Value of Int 2147483647
System.out.println("Int largest number is: " + Integer.MAX_VALUE);
errorCorrectInt("2147483648");
errorCorrectInt("10");
System.out.println();
// Number is smaller than Min_Value of Long -9223372036854775808
System.out.println("Long smallest number is: " + Long.MIN_VALUE);
numberFormatExceptLongMinValue("-9223372036854775809");
numberFormatExceptLongMinValue("10");
System.out.println();
ArrayIndexOOB(4);
ScannerTryCatch();
}
public static void numberFormatExceptMinValue(String number_str) {
System.out.println("We choose: " + number_str);
try {
// number = Integer.parseInt(sc.next());
int number = Integer.parseInt(number_str);
System.out.println(number + " is good");
} catch (NumberFormatException e) {
System.out.println(
"NumberFormatException occurred\n");
}
}
public static void numberFormatExceptMaxValue(String number_str) {
System.out.println("We choose: " + number_str);
try {
// number = Integer.parseInt(sc.next());
int number = Integer.parseInt(number_str);
System.out.println(number + " is good");
} catch (NumberFormatException e) {
// Print the message if exception occurred
// Number is larger than Max_Value of Int 2147483647
//Number is smaller than Min_Value of Int -2147483648
System.out.println(
"NumberFormatException occurred\n");
}
}
public static void ArrayIndexOOB(int number_str) {
int x = 0;
int[] myNumbers = {1, 2, 3};
System.out.println("This array has {1, 2, 3}");
while (x < number_str) {
try {
System.out.println(myNumbers[x]);
} catch (Exception e) {
System.out.println("ArrayIndexOutOfBoundsException\n");
}
x++;
}
}
public static void numberFormatExceptLongMinValue(String number_str) {
System.out.println("We choose: " + number_str);
try {
long number = Long.parseLong(number_str);
System.out.println(number + " is good");
} catch (NumberFormatException e) {
System.out.println(
"NumberFormatException occurred\n");
}
}
public static void errorCorrectInt(String number_str) {
if ((Integer.MIN_VALUE > Long.parseLong(number_str)) || (Integer.MAX_VALUE < Long.parseLong(number_str))) {
System.out.println(Long.parseLong(number_str) + " outside range of int");
} else {
numberFormatExceptMinValue(number_str);
}
System.out.println();
}
public static void ScannerTryCatch() {
Scanner scanner = new Scanner(System.in);
while ( true ) {
try {
System.out.println("Pick a number");
String number_str = scanner.nextLine();
int number = Integer.parseInt(number_str);
System.out.println(number + " is a number");
break;
} catch
(NumberFormatException e) {
System.out.println(" not a number");
}
}
}
}
C:\Users\netadmin\.jdks\openjdk-23.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\lib\idea_rt.jar=51426:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\netadmin\IdeaProjects\TryCatchPractice\out\production\TryCatchPractice Main
Int smallest number is: -2147483648
We choose: -2147483649
NumberFormatException occurred
We choose: 10
10 is good
Int smallest number is: -2147483648
-2147483649 outside range of int
We choose: 10
10 is good
Int largest number is: 2147483647
We choose: 2147483648
NumberFormatException occurred
We choose: 10
10 is good
Int largest number is: 2147483647
2147483648 outside range of int
We choose: 10
10 is good
Long smallest number is: -9223372036854775808
We choose: -9223372036854775809
NumberFormatException occurred
We choose: 10
10 is good
This array has {1, 2, 3}
1
2
3
ArrayIndexOutOfBoundsException
Pick a number
2147483648
not a number
Pick a number
-2147483649
not a number
Pick a number
2147483647
2147483647 is a number
Process finished with exit code 0