相关疑难解决方法(0)

抓住Throwable是不好的做法?

赶上是不好的做法Throwable

例如这样的事情:

try {
    // Some code
} catch(Throwable e) {
    // handle the exception
}
Run Code Online (Sandbox Code Playgroud)

这是一种不好的做法还是我们应该尽可能具体?

java exception-handling throwable

100
推荐指数
10
解决办法
8万
查看次数

使用关键字throws来声明抛出运行时异常

对我来说,这些代码

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)

java exception-handling

5
推荐指数
1
解决办法
844
查看次数

标签 统计

exception-handling ×2

java ×2

throwable ×1