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…

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…

Java Made Clear: Difference between throws and throw

For a novice, both throws and throw keywords are very confusing. "throws" and "throw" works differently in exception handling. "throws" Keyword is used in two ways To claim the exception As an alternative for try-catch "throw" Keyword is used to throw the exception object Let us go into details. I.…

InstantiationException Java

Following is the hierarchy. java.lang.Object –> java.lang.Throwable –> java.lang.Exception –> java.lang.InstantiationException The InstantiationException constructor is overloaded, apart default with another with string parameter. This exception was introduced with JDK 1.0, the starting version of Java. This exception is thrown by the JVM when it is unable to create an instance…

Rules of Exceptions in Multiple Catch Blocks

Generally, it is a good programming practice to write one problematic statement (that may throw an exception) in one try block. If the same one statement is prone to multiple problems, then it is required to write multiple catch blocks or by chance if the try block contains multiple problematic…

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 –>…

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

In the above program, Vector object vect is created.…

IllegalStateException

As the name indicates, this exception is thrown when the programmer is doing an operation that is illegal at the present moment (but legal at some other time or context). That is, in appropriate time a method is called, the JVM throws this exception. Examples for IllegalStateException are many in…

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.

Observe, in the while loop, both ListIterator (with it1) and ArrayList (with al1)…

IllegalThreadStateException

This is an unchecked exception and as the name indicates, is thrown by the Java runtime environment, when the programmer is trying to modify the state of the thread when it is illegal. It is also of commonsense, there is no meaning of starting a thread which is already started.…

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 –…

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…

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…

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.…

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…

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…

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,…

NumberFormatException

NumberFormatException is an unchecked exception thrown by parseXXX() methods when they are unable to format (convert) a string into a number. Sometimes, in Java coding, we get input (like from command-line arguments and text field) from the user in the form of string. To use the string in arithmetic operations,…

Java All Exceptions Tutorial

1. Exceptions – Meaning 2. Runtime Error – Groups – Exception causes 3. Exception Handling: try – catch 4. try – catch – finally 5. Hierarchy of Exceptions – Checked/Unchecked Exceptions 6. Throwing with "throws" – Alternative to try-catch 7. Creating User-defined Exceptions – throw Keyword 8. getMessage() & printStackTrace()…