Java Exception Handling
Java Exception Handling
+ 0 comments 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."); } else{ return (long)Math.pow(n,p); } } }
+ 0 comments Expected results contradict the condition for the input -1 -2 it shows "java.lang.Exception: n or p should not be negative." when it clearly states if both are negative it should throw exception "java.lang.Exception: n and p should not be negative"
+ 0 comments This is my code but don't know what is problem i failed in run code
import java.io.; import java.util.; 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 IOException("n and p should not be zero"); else
} return (long)Math.pow(n,p); } public class Solution {public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ MyCalculator mc = new MyCalculator(); Scanner sc = new Scanner(System.in); try{ int n=sc.nextInt(); int p=sc.nextInt(); System.out.println(mc.power(n,p)); } catch(Exception e){ System.out.println(e.getMessage()); } }
}
+ 1 comment Someone please help to correct. Getting failed test case and wrong answer-
class MyCalculator { public 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==00){ throw new Exception("n and p should not be zero"); } else{ return (long) Math.pow(n,p); } }
+ 0 comments import java.lang.; import java.math.; class MyCalculator { /* * Create the method long power(int, int) here. */ public 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."); }
long ans; ans=(long)Math.pow(n,p); return ans; }
}
Sort 264 Discussions, By:
Please Login in order to post a comment