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

HackerEarth Exception Handling problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Related website

The Computer Science

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes