赶上是不好的做法Throwable?
例如这样的事情:
try {
// Some code
} catch(Throwable e) {
// handle the exception
}
Run Code Online (Sandbox Code Playgroud)
这是一种不好的做法还是我们应该尽可能具体?
对我来说,这些代码
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;
}
}
Run Code Online (Sandbox Code Playgroud)
没有相同的代码
throws ArithmeticException
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码时,请执行相同的操作:
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 …Run Code Online (Sandbox Code Playgroud)