Day-to-Day Exceptions

ArrayIndexOutOfBoundsException Java Example

Introduction Generally, a fresher learning a programming language like C-lang, thinks that when his program is compiled, the output is guaranteed. With a bit of more practice, he comes to know that there may be problems raised by the system that prevents the program execution. These problems raised at runtime by the system (in Java, …

ArrayIndexOutOfBoundsException Java Example Read More »

ArithmeticException Handling Java Example

Introduction Generally, a fresher learning a programming language like C-lang, thinks that when his program is compiled, the output is guaranteed. With a bit of more practice, he comes to know that there may be problems raised by the system that prevents the program execution. These problems raised at runtime by the system (in Java, …

ArithmeticException Handling Java Example Read More »

NullPointerException

NullPointerException is an unchecked exception from java.lang package. As the name indicates, if an object points to null and further used in the code, the JVM throws NullPointerException. Following is the class signature public class NullPointerException extends RuntimeException Following is the hierarchy Object –> Throwable –> Exception –> RuntimeException –> NullPointerException NullPointerException can be thrown …

NullPointerException Read More »

Java NoSuchElementException

Java NoSuchElementException Java NoSuchElementException is raised when the element called does not exist in DS. It is an unchecked exception from java.util package. Following is the hierarchy. Object –> Throwable –> Exception –> RuntimeException –> NoSuchElementException Program on Java NoSuchElementException import java.util.Vector; public class NSE { public static void main(String args[]) { Vector vect = …

Java NoSuchElementException Read More »

ConcurrentModificationException

As the name indicates, this exception is thrown when two objects are modifying a DS (like Vector or ArrayList) concurrently (at the same time when an operation, like iteration, is going on). Observe, the following code. import java.util.*; public class IteratorDemo { public static void main(String args[]) { ArrayList al1 = new ArrayList (); al1.add(“Raju”); …

ConcurrentModificationException Read More »

ClassCastException

This is an unchecked exception as it is a subclass of RuntimeException. We know in exception handling, all the subclasses of RuntimeException are known as unchecked exceptions. Following is the hierarchy. Object –> Throwable –> Exception –> RuntimeException –> ClassCastException Complete exception hierarchy is available at Hierarchy of Exceptions – Checked/Unchecked Exceptions. As the name …

ClassCastException Read More »

RuntimeException

It is an unchecked exception derived from Exception. It comprises of a big bunch of subclasses and these all are known as unchecked exceptions like ArrayIndexOutOfBoundsException, ArithmeticException, NumberFormatException, ClassCastException etc. Following is the hierarchy. Object –> Throwable –> Exception –> RuntimeException Complete exception hierarchy is available at Hierarchy of Exceptions – Checked/Unchecked Exceptions. As it …

RuntimeException Read More »

MalformedURLException Java

MalformedURLException Java Summary: By the end you reach this tutorial you will understand the possibility of throwing MalformedURLException Java. The JDK 1.5 includes twelve exceptions used with network programming. The commonly occurring are MalformedURLException and UnknownHostException. Let us when they are thrown in coding. MalformedURLException is a checked exception. It is inherited from IOException. MalformedURLException …

MalformedURLException Java Read More »

UnknownHostException Java

Java UnknownHostException It is a subclass of IOException. This exception is thrown when the IP address of the other system (say, server) is not found by the networking software. No confusion here, between MalformedURLException and UnknownHostException. Wrong format is MalformedURLException and unable to locate in the network it is UnknownHostException. Following is the hierarchy. Object …

UnknownHostException Java Read More »

IllegalArgumentException

IllegalArgumentException It is an unchecked exception a subclass of RuntimeException. It is thrown by Color constructor when wrong parameters are passed. Observe the syntax of java.awt.Color constructor. Color clr1 = new Color(int red, int green, int blue); The RGB values should be within the range of 0 to 255 (inclusive of both). If other values …

IllegalArgumentException Read More »

NoSuchMethodError

It is a checked exception, generally we experience, when we try to execute a class without main() method. Following is the hierarchy of NoSuchMethodError. Object –> Throwable –> Error –> LinkageError –> IncompatibleClassChangeError –> NoSuchMethodError Full hierarchy of exceptions is available at "Hierarchy of Exceptions – Checked/Unchecked Exceptions". Following program illustrates. public class Demo { …

NoSuchMethodError Read More »

ClassNotFoundException

ClassNotFoundException is a checked exception from java.lang package, you get generally when you load a class manually with forName() method of class Class. This you experience in JDBC (Java Database Connectivity) where you load the driver from hard disk. Observe the following snippet of code. Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); In the above statement, forName() method of class Class …

ClassNotFoundException Read More »

IndexOutOfBoundsException

IndexOutOfBoundsException Summary: In this tutorial, you learn handling of IndexOutOfBoundsException with its subclasses. IndexOutOfBoundsException is an unchecked exception that extends RuntimeException. This is used by the JVM to check the index number entered by the user is within the range of an array or string. Full hierarchy of exceptions is available at "Hierarchy of Exceptions …

IndexOutOfBoundsException Read More »