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

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

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)

{

super(s);

}
}
class Check

{
public static void main(String[] args)

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

{
try

{
thrower(args[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

{
try

{
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;
}
finally
{
System.out.println("[thrower(\"" + s +
"\") done]");
}
}

}

Output:

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

[thrower("divide") done]

C:\saurabh>javac Check.java
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]; 

System.out.println(string[1].charAt(1));


}

}

Output:

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

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

  • Java.io.FileNotFoundException

Java.io.FileNotFoundException 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.

Resources:

Java Exception - Handle Exceptions in Java

Unhandled Exception

Java file not found exception

Java Catching and Handling Exceptions