Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Exception Handling Try-catch-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Exception Handling Try-catch – Hacker Rank Solution
Java Exception Handling Try-catch – Hacker Rank Solution
Objective
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.
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:
10 3
Sample Output 0:
3
Sample Input 1:
10 Hello
Sample Output 1:
java.util.InputMismatchException
Sample Input 2:
10 0
Sample Output 2:
java.lang.ArithmeticException: / by zero
Sample Input 3:
23.323 0
Sample Output 3:
java.util.InputMismatchException
Java Exception Handling Try-catch – Hacker Rank Solution
import java.util.Scanner; class MyCalculator { long power(int n, int p) throws Exception { if (n < 0 || p < 0) { throw new Exception("n or p should not be negative."); } else if (n == 0 && p == 0) { throw new Exception("n and p should not be zero."); } return (long) Math.pow(n, p); } } public class Solution { public static final MyCalculator my_calculator = new MyCalculator(); public static final Scanner in = new Scanner(System.in); public static void main(String[] args) { while (in .hasNextInt()) { int n = in .nextInt(); int p = in .nextInt(); try { System.out.println(my_calculator.power(n, p)); } catch (Exception e) { System.out.println(e); } } } }
Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.