This is for an online tutorial in throwing exceptions
I am trying to do something like this:
int power(int n, int p){
try
{
return (int)Math.pow(n,p);
}
catch(Exception e)
{
throw new Exception("n and p should be non-negative");
}
}
But I get the error
error: unreported exception Exception; must be caught or declared to be thrown
答案 0 :(得分:4)
Exception
is a checked exception, which means that if a method wants to throw it, it must declare it in a throws
clause.
int power(int n, int p) throws Exception {
try
{
return (int)Math.pow(n,p);
}
catch(Exception e)
{
throw new Exception("n and p should be non-negative");
}
}