For me, these code
static int faktorial (int n) throws ArithmeticException {
if ((n < 0) || (n > 31)) {
throw new ArithmeticException();
}
if (n > 1) {
return n * faktorial(n - 1);
}
else {
return 1;
}
}
and the same code without
throws ArithmeticException
do the same, when I use it with the following code:
public static void main(String[] args) {
try {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Insert integer number: ");
n = sc.nextInt();
System.out.println(n + "! = " + faktorial(n));
}
catch (InputMismatchException e) {
System.out.println("Not an integer number!");
e. printStackTrace();
}
catch (RuntimeException e) {
System.out.println("Number is too big!");
e. printStackTrace();
}
}
Could someone describe me if use of
throws ArithmeticException
has some advantages in my code.
I will also appreciate some good example of using keyword throws
. Thank you very much!