Program – simulate or generate unchecked exceptions (java & examples)

1. What is unchecked exception in java?

  1. Unchecked exceptions are not checked at compiled time.
  2. The unchecked exceptions are not forced by compiler to implement.
    1. i.e. Its not mandatory to enclose the code with try-catch block.
    2. Its purely at programmer’s discretion to catch or throws the unchecked exceptions.
  3. We have already discussed about the checked and unchecked exceptions in java.

2. Examples of unchecked exceptions in java

  1. ArithmeticException
  2. NegativeArraySizeException
  3. NullPointerException
  4. StringIndexOutOfBoundsException
  5. NumberFormatException
  6. StackOverflowError.

3. Program – simulate or generate unchecked exceptions in java (example)

package org.learn;

public class UnCheckedExceptionDemo {
 public static void main(String[] args) {
  catchArithmeticException();
  catchNegativeArraySizeException();
  catchNullPointerException();
  catchStringIndexOutOfBoundsException();
  catchNumberFormatException();
  stackOverflowErrorFunction();
 }

 private static void catchArithmeticException() {
  try {
   int value = 15;
   int divideByZero = value / 0;
   System.out.println("Result of division by zero is " + divideByZero);
  } catch (ArithmeticException arithmeticException) {
   System.out.println("1. Exception occurred is:" + arithmeticException.getLocalizedMessage());
  }
 }

 private static void catchNegativeArraySizeException() {
  try {
   int intArray[] = new int[-5];
   for (int index = 0; index < intArray.length; index++) {
    intArray[index] = 0;
   }
  } catch (NegativeArraySizeException nase) {
   System.out.println("2. Exception occured is NegativeArraySizeException");
  }
 }

 @SuppressWarnings("null" )
 private static void catchNullPointerException() {
  try {
   Integer integer = null;
   int intValue = integer.intValue();
   System.out.println("Parse int result is: " + intValue);
  } catch (NullPointerException npe) {
   System.out.println("3. Exception occurred is NullPointerException");
  }
 }

 private static void catchStringIndexOutOfBoundsException() {
  try {
   String sport = "Soccer";
   char lastChar = sport.charAt(sport.length());
   System.out.println("Last character of sport is:" + lastChar);
  } catch (StringIndexOutOfBoundsException siob) {
   System.out.println("4. Exception occurred due to " + siob.getLocalizedMessage());
  }
 }

 private static void catchNumberFormatException() {
  try {
   String number = "InvalidNumber";
   long formattedNumber = Long.valueOf(number);
   System.out.println("Result of formating string is:" + formattedNumber);
  } catch (NumberFormatException numForExe) {
   System.out.println("5. Exception occurred " + numForExe.getLocalizedMessage());
  }
 }

 private static void stackOverflowErrorFunction() {
  stackOverflowErrorFunction();
 }
}

Output – simulate or generate unchecked exceptions in java (example)

1. Exception occurred is:/ by zero
2. Exception occured is NegativeArraySizeException
3. Exception occurred is NullPointerException
4. Exception occurred due to String index out of range: 6
5. Exception occurred For input string: "InvalidNumber"
Exception in thread "main" java.lang.StackOverflowError
 at org.learn.UnCheckedExceptionDemo.stackOverflowErrorFunction(UnCheckedExceptionDemo.java:66)
 at org.learn.UnCheckedExceptionDemo.stackOverflowErrorFunction(UnCheckedExceptionDemo.java:66)
 at org.learn.UnCheckedExceptionDemo.stackOverflowErrorFunction(UnCheckedExceptionDemo.java:66)
 at org.learn.UnCheckedExceptionDemo.stackOverflowErrorFunction(UnCheckedExceptionDemo.java:66)
        ..........................
Scroll to Top