Exception Handling Program in Java for 2025
For freshers and having 1 to 3 years of experience, we have listed the most important 25+ exception handling program in Java with output for the best practices in 2025.
These kinds of exception handling programming coding questions are always asked in Java technical tests and interviews. Your concepts will be more clear on this topic if you are able to solve 60% to 70% of these interview programming questions.
If you need any explanation to clear concepts, you can follow exception handling tutorials related to the questions.
Programming Questions on Exception Handling in Java
1. What kind of checked or unchecked exception will the following programs throw, if any?
a)
public class Test {
public static void main(String[] args)
{
int x = 10;
int y = 0;
int z = x / y;
System.out.println(z);
}
}
Output: ArithmeticException
b)
public class Test {
public static void main(String[] args)
{
double n[] = {3.2, 3, 4, 6};
int a = 4;
double x = n[0]/(a - n[2]);
System.out.println(x);
}
}
Output: Infinity
c)
public class Test {
public static void main(String[] args)
{
String str = "Scientech";
System.out.println(str.charAt(9));
}
}
Output: StringIndexOutOfBoundsException
d)
public class Test {
public static void main(String[] args)
{
Object obj = new Object();
String str = (String)obj;
System.out.println(str);
}
}
Output: ClassCastException
e)
public class Test {
public static void main(String[] args)
{
System.out.println("Hello");
Object obj = null;
System.out.println(obj.toString());
}
}
Output: NullPointerException
f)
public class Test {
public static void main(String[] args)
{
System.out.println("Hello Java");
int[] list = {1, 2, 3, 4, 5};
System.out.println(list[5]);
}
}
Output: ArrayIndexOutOfBoundsException
g)
public class Test {
public static void main(String[] args)
{
String str = "Java Programming";
int a = Integer.parseInt(str);
System.out.println("Value of a: " +a);
}
}
Output: NumberFormatException
h)
public class Test {
public static void main(String[] args)
{
byte num;
System.out.println("Scientech");
num = Byte.parseByte("Easy");
System.out.println("num: " +num);
}
}
Output: Scientech, NumberFormatException
Recommended Interview Tutorials
- Top 65+ Java Exceptional Handling Interview Questions with Answers
- Java Method Overloading Interview Programs for Practice
- Top 15 Method Overriding Interview Programs for Practice
- Top 10 Inheritance Interview Programs for Practice
i)
public class Test {
public static void main(String[] args)
{
for(int i = 1; i <= 5; i++)
System.out.println("Value of i: " +i);
Thread.sleep(1000);
System.out.println("Hello Java");
}
}
Ouput: InterruptedException
j)
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main(String[] args)
{
File file = new File("not_existing_file.txt");
FileInputStream stream = new FileInputStream(file);
}
}
Output: FileNotFoundException
2. What will be the output of the following program when run?
public class Test {
public static void main(String[] args)
{
try {
int value = 5;
if (value < 10)
throw new RuntimeException("Value is less than 10");
}
catch (RuntimeException re) {
System.out.println(re.getMessage());
}
System.out.println("Outside try-catch block");
}
}
Output: Value is less than 10, Outside try-catch block
[adinserter block=”2″]
3. What would be the output in the program 2 if the line int value = 5; is changed to int value = 20;?
Output: Outside try-catch block
4. What would be the output of the following programs?
a)
public class Test {
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
try {
int a = 10;
int b = 0;
int c = a/b;
System.out.println(c);
}
catch (ArithmeticException ae) { }
}
}
}
Output: 1 2 3 4 5
b)
class Test {
public static void main(String[] args)
{
try {
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
int a = 20;
int b = 0;
int c = a/b;
System.out.println(c);
}
}
catch (ArithmeticException ae) { }
}
}
Output: 1
c)
public class Test {
public static void main(String[] args)
{
try {
for (int i = 1; i <= 5; i++)
{
System.out.print(i + " ");
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("\nOut of try-catch block");
}
}
Output: 1 2 3 4 5 , Out of try-catch block
d)
class Test {
public static void main(String[] args)
{
System.out.println("111");
try {
int x = 12/0;
System.out.println("Result of x: " +x);
System.out.println("333");
}
catch(ArithmeticException ae)
{
System.out.println("Hello world");
}
System.out.println("444");
}
}
Output: 111, Hello world, 444
e)
class Test {
int x = 30, y = 0;
void divide()
{
System.out.println("I am in method");
try {
System.out.println("I am in try block");
int z = x / y;
System.out.println("Result of z: " +z);
}
catch(NullPointerException np)
{
System.out.println("I am in catch block");
}
}
public static void main(String[] args)
{
Test t = new Test();
t.divide();
}
}
Output: I am in method, I am in try block, Exception in thread “main” java.lang.ArithmeticException: / by zero
f)
class Test {
public static void main(String[] args)
{
System.out.println("111");
try {
System.out.println("222");
double y = 1.0/0;
}
catch(ArithmeticException e)
{
try {
System.out.println("Hello");
int x = 20/0;
}
catch(NullPointerException np)
{
System.out.println("333");
}
}
System.out.println("444");
}
}
Output: 111, 222, 444
g)
class Test {
public static void main(String[] args)
{
System.out.println("111");
try {
System.out.println("222");
int y = 1/0;
}
catch(ArithmeticException e)
{
try {
System.out.println("Hello");
double x = 2.5/0;
System.out.println("Java");
}
catch(NullPointerException np)
{
System.out.println("333");
}
}
System.out.println("444");
}
}
Output: 111, 222, Hello, Java, 444
h)
class Test {
public static void main(String[] args)
{
System.out.println("111");
try {
System.out.println("222");
}
catch(ArithmeticException e)
{
try {
double x = 2/0;
}
catch(NullPointerException np) { }
}
System.out.println("ABC");
}
}
Output: 111, 222, ABC