We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Java
  3. Exception Handling
  4. Java Exception Handling
  5. Discussions

Java Exception Handling

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • siddhantsingh
    4 years ago+ 0 comments
    class MyCalculator{
        public static int 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.");
            }
            else {
                return ((int)Math.pow(n,p));
            }
        }
    }
    
    10|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature