我应该捕捉一个方法抛出的所有异常吗? [英] Should i catch all the exception which are thrown by a method?

查看:200
本文介绍了我应该捕捉一个方法抛出的所有异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  try {
output.format(%d%s%s%.2f%n,input.nextInt(),
input.next ),input.next(),input.nextDouble());
} catch(FormatterClosedException formatterClosedException){
System.err.println(错误写入文件终止);
break;
} catch(NoSuchElementException noSuchElementException){
System.err.println(Invalid input。Please try again。);
input.nextLine();
}

方法格式(String format,Object .. 。args) Formatter class throws two exception IllegalFormatException FormatterClosedException ,但在我的书中,上述代码捕获 NoSuchElementException FormatterClosedException


  1. 为什么代码捕获 NoSuchElementException 但没有捕获 IllegalFormatException

  2. 如果我们需要捕获 NoSuchElementException ,如果在Formatter类格式()方法中甚至没有指定在线文档


解决方案


文档 java.util.NoSuchElementException 是一个 RuntimeException ,可以由Java中的不同类别抛出
像Iterator,Enumerator, Scanner
或StringTokenizer。


在你的情况下,它是扫描器。它不是从格式方法。



这是只是为了更安全如果没有给出下一个输入,那么抛出这个异常)。



演示演示的示例代码

  public class NoSuchElementExceptionDemo {
public static void main(String args []){
Hashtable sampleMap = new Hashtable();
枚举枚举= sampleMap.elements();
enumeration.nextElement(); //java.util.NoSuchElementExcepiton这里因为枚举是空的
}
}

输出:
线程main中的异常java.util.NoSuchElementException:Hashtable枚举器
在java.util.Hashtable $ EmptyEnumerator.nextElement(Hashtable.java:1084)
在test.ExceptionTest.main(NoSuchElementExceptionDemo.java:23)
/ pre>

try{
   output.format("%d %s %s %.2f%n", input.nextInt(),
        input.next(),input.next(),input.nextDouble());
} catch(FormatterClosedException formatterClosedException){
    System.err.println("Error writing to file. Terminating.");
    break;
} catch(NoSuchElementException noSuchElementException){
    System.err.println("Invalid input. Please try again.");
    input.nextLine();
}

The method format(String format, Object... args) in Formatter class throws two exception: IllegalFormatException and FormatterClosedException but in my book the above code catches NoSuchElementException and FormatterClosedException.

  1. Why did the code catch NoSuchElementException but did not catch IllegalFormatException?
  2. How can we know if we need to catch NoSuchElementException if it is not even stated in the Formatter class format() method in the online documentation ?

解决方案

Document: java.util.NoSuchElementException is a RuntimeException which can be thrown by different classes in Java like Iterator, Enumerator, Scanner or StringTokenizer.

In your case it is Scanner. Its not from format method.

It's just to be in the safer side(if next input is not given then throw this exception).

Sample code which shows the demo

public class NoSuchElementExceptionDemo{
    public static void main(String args[]) {
        Hashtable sampleMap = new Hashtable();
        Enumeration enumeration = sampleMap.elements();
        enumeration.nextElement();  //java.util.NoSuchElementExcepiton here because enumeration is empty
    }
}

Output:
Exception in thread "main" java.util.NoSuchElementException: Hashtable Enumerator
        at java.util.Hashtable$EmptyEnumerator.nextElement(Hashtable.java:1084)
        at test.ExceptionTest.main(NoSuchElementExceptionDemo.java:23)

这篇关于我应该捕捉一个方法抛出的所有异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆