Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Exception Handling problem solution

YASH PAL, 31 July 202413 February 2026

In this HackerEarth Exception Handling problem solution, you are given a piece of Java code. You have to complete the code by writing down the handlers for exceptions thrown by the code. The exceptions the code may throw along with the handler message are listed below:

 
Division by zero: Print “Invalid division”.
String parsed to a numeric variable: Print “Format mismatch”.
Accessing an invalid index in the string: Print “Index is invalid”.
Accessing an invalid index in the array: Print “Array index is invalid”.
 
MyException: This is a user-defined Exception which you need to create. It takes a parameter param. When an exception of this class is encountered, the handler should print “MyException[param]”, here param is the parameter passed to the exception class.
 
Exceptions other than mentioned above: Any other exception except the above ones fall in this category. Print “Exception encountered”.
 
Finally, after the exception is handled, print “Exception Handling Completed”.
 
 
HackerEarth Exception Handling problem solution
 

HackerEarth Exception Handling problem solution.

import java.util.Scanner;
import java.lang.Exception;
class TestClass
{
  static void solve(int arr[]) throws Exception
  {
    int ans=0;
    
    for(int i=0;i<10;i++)
      for(int j=i+1;j<10;j++)
        ans+=arr[i]/arr[j];
    
    throw new MyException(ans);
  }
  public static void main(String args[])
  {
    try
    {
      Scanner in=new Scanner(System.in);
      int n=in.nextInt();
      int []arr= new int[n];
      in.nextLine();
      for(int i=0;i<10;i++)
        arr[i]=Integer.parseInt(in.nextLine());
      
      String s=in.nextLine();
      System.out.println(s.charAt(10));
      
      solve(arr);
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
      System.out.println("Array index is invalid");
    }
    catch(NumberFormatException e)
    {
      System.out.println("Format mismatch");
    }
    catch(StringIndexOutOfBoundsException e)
    {
      System.out.println("Index is invalid");
    }
    catch(ArithmeticException e)
    {
      System.out.println("Invalid division");
    }
    catch(MyException e)
    {
      System.out.println(e);
    }
    catch(Exception e)
    {
      System.out.println("Exception encountered");
    }
    finally
    {
      System.out.println("Exception Handling Completed");
    }
  }
}
class MyException extends Exception
{
  private int detail;
  MyException(int a)
  {
    detail=a;
  }
  public String toString()
  {
    return "MyException["+detail+"]";
  }
}
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes