在 Java 中抛出一般异常
Throwing general exception in Java
这是抛出异常的在线教程
我正在尝试做这样的事情:
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");
}
}
但我收到错误
error: unreported exception Exception; must be caught or declared to be thrown
Exception
是checked exception,也就是说如果一个方法要抛出它,必须在throws
子句中声明。
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");
}
}
这是抛出异常的在线教程
我正在尝试做这样的事情:
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");
}
}
但我收到错误
error: unreported exception Exception; must be caught or declared to be thrown
Exception
是checked exception,也就是说如果一个方法要抛出它,必须在throws
子句中声明。
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");
}
}