DEV Community

Indumathy
Indumathy

Posted on

Exception handling scenario

1. Division Program :
public class DivisionTest {
public static void main(String[] args) {
int a = 10;
int b = 0;

    int result = a / b;
    System.out.println("Result: " + result);
}
Enter fullscreen mode Exit fullscreen mode

}

Solution for exception:

Here we cant divide number by zero. This is caught at runtime. so arithmetic exception will comes under unchecked exception. Below is the code with handled exception.

public class DivisionTest {

public static void main(String\[] args) {

int a = 10;

int b = 0;

try {

       int result = a / b;

      System.out.println("Results: " + result);

   } 

  catch (ArithmeticException e) {

     System.out.println("Exception caught: Cannot divide by zero");

 }

}

}
Enter fullscreen mode Exit fullscreen mode

2. Array Index Program :
public class ArrayTest {
public static void main(String[] args) {
int arr[] = {10, 20, 30, 40};

    System.out.println(arr[5]);
}
Enter fullscreen mode Exit fullscreen mode

}

Solution:

Here the array index value is only 4. calling the 5th index will cause the ArrayIndexOutOfBoundsException. This comes under unchecked exception. This occurs at runtime only.

public class ArrayTest {

public static void main(String\[] args) {

  int arr\[] = {10, 20, 30, 40};



 try {

        System.out.println(arr\[5]);

  } 

 catch (ArrayIndexOutOfBoundsException e) {

   System.out.println("Invalid array index");

     }

}
}
Enter fullscreen mode Exit fullscreen mode

3. String Conversion Program :
public class NumberFormatTest {
public static void main(String[] args) {
String s = "ABC";

    int num = Integer.parseInt(s);
    System.out.println(num);
}
Enter fullscreen mode Exit fullscreen mode

}

Solution:

Integer.parseInt(); -> This Method is used for converting string into int data type. It works only when the string contains numeric value. so here we will face NumberFormatException.

which occurs on runtime. So this is a unchecked exception.

public class NumberFormatTest {

public static void main(String[] args) {

String s = "ABC";

try { int num = Integer.parseInt(s);

System.out.println(num);}

catch(NumberFormatException e) {

   System.out.println("Unable to convert string to number");

}
Enter fullscreen mode Exit fullscreen mode

}

}

  1. NullPointer Program :
    public class NullTest {
    public static void main(String[] args) {
    String name = null;

    System.out.println(name.length());
    

    }
    }

Solution:

When the variable value is Null we cant use the length() method. we will get the NullPointerException. This occurs due to runtime. so it unchecked exception.

public class NullTest {

   public static void main(String\[] args) {

   String name = null;

   try { System.out.println(name.length());}

    catch(NullPointerException e) 

   {System.out.println("Name value should not be Null");

      }

}

}

Enter fullscreen mode Exit fullscreen mode

5. File Reading Program :
public class FileTest {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("test.txt");

    int data = fr.read();
    System.out.println(data);
}
Enter fullscreen mode Exit fullscreen mode

}

Solution:

Here we request to read text from a file. Whether the file may exists or not. So, here we can expect the FileNotFoundException. This might be predicted and handled in compile time. This is checked exception.

public class FileTest {
    public static void main(String[] args) throws Exception {
       try{
         FileReader fr = new FileReader("test.txt");

        int data = fr.read();
        System.out.println(data);
}
catch(FileNotFoundException e)
{
System.out.println("File not Found in given path");
}
}
}

Enter fullscreen mode Exit fullscreen mode

6. String Index Program :
public class StringIndexTest {
public static void main(String[] args) {
String str = "Java";

    System.out.println(str.charAt(10));
}
Enter fullscreen mode Exit fullscreen mode

}

Solution:

Here the string length is only 4. But here we are calling the 10th index. So we will receive the StringIndexOutOfBoundsException. This is unchecked exception.

public class StringIndexTest {
    public static void main(String[] args) {
        String str = "Java";

       try
{ System.out.println(str.charAt(10));
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("Index out of range");
}
    }
}
Enter fullscreen mode Exit fullscreen mode

7. Negative Array Size :
public class NegativeArrayTest {
public static void main(String[] args) {
int size = -5;

    int arr[] = new int[size];
}
Enter fullscreen mode Exit fullscreen mode

}

Solution:

Array size cannot be in negative value.

  • NegativeArraySizeException.
  • It is an Unchecked Exception (Runtime Exception).
public class NegativeArrayTest {
    public static void main(String[] args) {
        int size = -5;

        try {
            int arr[] = new int[size];
        } 
        catch (NegativeArraySizeException e) {
            System.out.println("Exception caught: Array size cannot be negative");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

8.TreeSet Programs :
public class Test
{
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(10);
set.add(30);
set.add("hari");
System.out.println(set);
}
}

Solution:

TreeSet will give give the values in sorted order. While comparing the value we will receive the ClassCastException. This will occurs in runtime. so this belongs to unchecked exception.

import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();

        try {
            set.add(10);
            set.add(30);
            set.add("hari");   // different data type
            System.out.println(set);
        } 
        catch (ClassCastException e) {
            System.out.println("Cannot add different data types in TreeSet");
        }
    }

Enter fullscreen mode Exit fullscreen mode

9.TreeSet programs :
public class Test
{
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(20);
set.add(40);
set.add(null);

}
}

Solution:

TreeSet stores elements in sorted order, so it compares elements.
When you add null, Java cannot compare it with other elements.

public class Test {
    public static void main(String[] args) {
        TreeSet set = new TreeSet();

        try {
            set.add(20);
            set.add(40);
            set.add(null);
        } 
        catch (NullPointerException e) {
            System.out.println("Exception caught: TreeSet does not allow null values");
        }

        System.out.println(set);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)