0
Sponsored Links


Ad by Google
In my previous post we have seen, what is Checked Exception and in this post, I shall try to explain what is Runtime exception and list few runtime exceptions. Runtime exception is also known as un-checked exception, so un-checked exception is also very popular topic in core java interview like How to balance the parenthesis and java program to find missing number.
What is Runtime Exception ?
The Runtime exceptions are basically a bug in your code. It's developer's fault/mistake, you can easily prevent the run time exception in your code no rocket science is required you just need to do code in a smart way. We will see later in this tutorial "How to prevent the runtime exception", but before that, I strongly suggest you to must read What is Exception in java

The java.lang.RuntimeException is a sub-class of java.lang.Exception class and all the sub-class of java.lang.RuntimeException is treated as runtime exception and application cannot recover from these exception and it's not a part of catch or throws exception unlike checked exception, because java.lang.RuntimeException is not checked at compile time it's all about runtime.

How to prevent Runtime Exception
While coding never assume anything do unit testing of your code. The Runtime exception is all about bugs, like logical error in code or improper us of java api. Most of the time, some developer's forget to check the null before processing, forget to check an array length before accessing elements from an array etc. and that caused the error of java.lang.NullPointerException and java.lang.ArrayIndexOutOfBoundsException these all are Runtime exception.

I strongly recommend to do below checks while coding -
  1. While playing with an array must check the length of an array.
  2. While doing arithmetic operation must check the proper value of operands.
  3. Before casting object must performed an instanceof check.
  4. Before putting elements into Queue/Stack must check the target buffer's limit. 
  5. Before storing object in array must check the proper type of the object.
  6. Before taking an element from the Queue/Stack must check the buffer's limit.
  7. Ensure that concurrent modification not happens.
  8. Always check not null value before processing the not null value.
The above few points will helps you to reduce at least some popular programming fault.
Let's see an example without checking any points -
package com.javamakeuse.poc;

public class ExceptionExample {
 private static int[] numArray;

 static {
  numArray = new int[] { 1, 2, 3 };
 }
 public static void getElement(int index) {
  System.out.println("Value from an array - " + numArray[index]);
 }
 public static void main(String[] args) {
  getElement(4);
 }
}

The numArray length is 2 because array start from 0th index, so the accessing element of index 3rd will gives an error with ArrayIndexOutOfBoundsException.
OUT PUT:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:3
at com.javamakeuse.poc.ExceptionExample.getElement(ExceptionExample.java:12)
at com.javamakeuse.poc.ExceptionExample.main(ExceptionExample.java:16)

Now let's fixed this issue by checking the length of an array.
public static void getElement(int index) {
  if (numArray.length < index) {
   System.out.println("Value from an array - " + numArray[index]);
  } else {
   System.out.println("Array length is smaller than requested index - "+index);
  }
 }
OUT PUT:
Array length is smaller than requested index - 3

Few popular Runtime Exceptions -
  1. ArithmeticException 
  2. ArrayStoreException
  3. BufferOverflowException
  4. BufferUnderflowException
  5. ClassCastException
  6. ConcurrentModificationException
  7. IllegalArgumentException
  8. IllegalMonitorStateException
  9. IndexOutOfBoundsException
  10. NullPointerException


That's it,

Please write your comments/feedback, or if you want's to add some more points.
Sponsored Links

0 comments:

Post a Comment