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

Sort 220 Discussions, By:

votes

Please Login in order to post a comment

  • btamada
    6 years ago+ 1 comment

    More exception handling problems please :)

    63|
    Permalink
  • Disruption
    6 years ago+ 0 comments

    This problem is shameful, for a lot of reasons, some of them already exposed below.

    We can not teach people to use Java with a problem that makes so many basic errors (Class name starting in lowercase, variable with a single letter and in uppercase, which is usually reserved for constants, bad usage of try-catch for controlling the flow of the program, etc.).

    I though there was a review process or something for problems, but I have seen a few problems in the Java section that make me think otherwise...

    19|
    Permalink
  • vipularyaa
    4 years ago+ 2 comments
    public long power(int n, int p) throws Exception{
        long result;
        if(n==0 && p==0){
            throw new Exception("n and p should not be zero.");
        }
        if(n<0 || p<0){
            throw new Exception("n or p should not be negative.");
        }
             result=(long)Math.pow(n,p);
             return result;
    }
    
    14|
    Permalink
  • 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
  • joethomas89
    6 years ago+ 12 comments

    Hope this helps..

    class MyCalculator{
        public static int power(int n, int p) throws Exception{
            if(n < 0 || p < 0){
                throw new Exception ("n and p should be non-negative");
            } else {
                return ((int)Math.pow(n,p));
            }
        }
    }
    
    7|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature