Share on Google+Share on Google+

Unhandled Exception in Thread

There are different types of exception that are thrown during the execution of program due to mistake carried out by client. Here are examples that display the exceptions thrown and how to handle them.

Unhandled Exception in thread are thrown during the execution of the program when there is a mistake made by the client. Most of this exception can be handled using try and catch block but some cannot be handled by it. The only way to rectify it is to change it.

Example of Unhandled exception in Thread:

class ExceptionCheck extends Exception
ExceptionCheck ( )

super( );

ExceptionCheck(String s)



class Check

public static void main(String[] args)

for (int j = 0; j < args.length; j++)


System.out.println("Check\"" + args[j] +
"\" didn't throw an exception");

catch (Exception e)

System.out.println("Check\"" + args[j] +
"\" threw a " + e.getClass() +
"\n with message: " + e.getMessage());
static int thrower(String s) throws ExceptionCheck


if (s.equals("divide"))

int j = 0;
return j/j;
if (s.equals("null")) {
s = null;
return s.length();
if (s.equals("check"))
throw new CheckException("Test message");
return 0;
System.out.println("[thrower(\"" + s +
"\") done]");



If we pass divide null not test passed as argument to the method ,Command Prompt will display:

[thrower("divide") done]

C:\saurabh java Check "divide" threw a class java.lang.ArithmeticException
with message: / by zero
[thrower("null") done]
Check "null" threw a class java.lang.NullPointerException
with message: null
[thrower("not") done]
Check "not" didn't throw an exception
[thrower("check") done]
Test "check" threw a class CheckException
with message: Test message

Unhandled Exception in Thread are of different types :

  • java.lang.nullpointerexception

java.lang.nullpointerexception is thrown by JVM when a programmer calls a null instead of an abject of the method. There is not way to avoid the exception, one just has to stop calling null. try and catch block won't be useful here.

Here is an example explaining java.lang.nullpointerexception:

We have used a class javaerrornullpointerexception. string.charAt( ) method return you the position of the character specified by the index of the parameter. When we execute this program, an error java.lang is shown.

public class javaerrornullpointerexception {

public static void main(String args[]) {

String string[] = new String[5]; 





Compiling 1 source file to /home/girish/NetBeansProjects/errors/build/classes
Exception in thread "main" java.lang.NullPointerException
at javaerrornullpointerexception.main(
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

To resolve this error you have to initialise array as shown string[1]="dgdsf"

  • is thrown when a specified file is not found because it does not exist at the given pathname or fails to open.

This exception is thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors.

  • Java.lang.illegalargumentException

Java.lang.illegalargumentException is thrown when an illegal argument is sent to the method.


Java Exception - Handle Exceptions in Java

Unhandled Exception

Java file not found exception

Java Catching and Handling Exceptions


Posted on: October 3, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Unhandled Exception in Thread  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image