Hackerrank 30 days of code Java Solution: Day 17: More Exceptions

The next problem is also based on Exceptions.As told by me in post for day 16, exceptions are very important part of developer life. It teaches you the hard reality of programming.Exception handling is highly recommended and integral part of programming world. Its main objective is to end a program or a flow “Gracefully”. Java allow us to throw and catch custom exceptions.

Few important questions we must be aware of regarding the exception handling which are also very essential from interview point of view:

  • Propagating Exceptions
  • Types of Exception
  • When, where and how to handle exceptions.
  • Need of handling exceptions
  • Difference between exceptions and error?
  • What are the rules we need to follow when overriding a method that throws an exception?

So now lets jump to the problem statement of Day 17


Write a Calculator class with a single method: int power(int,int). The power method takes two integers, and , as parameters and returns the integer result of . If either or is negative, then the method must throw an exception with the message: n and p should be non-negative.

Sample Input

Sample Output

Solution to the problem is :

Thanks for the visit checkout the day 18 tutorial as well.. 🙂

Leave a Reply

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