Problem Name: Java Exception Handling (Try-catch)

Problem Link:

In this HackerRank Functions in Java programming problem solution,

Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. (Wikipedia)



Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of code for errors. The catch block contains the code that says what to do if exception occurs.


This problem will test your knowledge on try-catch block.

You will be given two integers x and y as input, you have to compute x/y . If x and y are not 32 bit signed integers or if y is zero, exception will occur and you have to report it. Read sample Input/Output to know what to report in case of exceptions.

Sample Input 0:


Sample Output 0:





Code Examples

#1 Code Example with Java Programming

Code - Java Programming

import java.util.Scanner;
import java.util.InputMismatchException;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(;
        try {
            int x = scan.nextInt();
            int y = scan.nextInt();
        } catch (InputMismatchException e) {
        } catch (ArithmeticException e) {
            System.out.println(e.getClass().getName() + ": / by zero");
Copy The Code & Try With Live Editor


[Solved] Java Iterator in Java solution in Hackerrank - Hacerrank solution Java
[Solved] Java Exception Handling in Java solution in Hackerrank - Hacerrank solution Java