if without the throws IllegalArgumentException
, I can still throw exception that catched by the catch block in the main fucntion, why I need throws IllegalArgumentException
??
public class ExceptionsTest {
public static void funcThrowsException(int n) throws IllegalArgumentException{
if(n == 0){
throw new IllegalArgumentException("ex");
}
}
public static void main(String[] args) {
try{
funcThrowsException(0);
}catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}
public class ExceptionsTest {
public static void funcThrowsException(int n){
if(n == 0){
throw new IllegalArgumentException("ex");
}
}
public static void main(String[] args) {
try{
funcThrowsException(0);
}catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}
Their output are same:
ex
Process finished with exit code 0
I don't know why we need throws keywords