我应该捕捉一个方法抛出的所有异常吗? [英] Should i catch all the exception which are thrown by a method?
问题描述
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
。
- 为什么代码捕获
NoSuchElementException
但没有捕获IllegalFormatException
? - 如果我们需要捕获
NoSuchElementException
,如果在Formatter类格式()方法中甚至没有指定在线文档?
文档:
java.util.NoSuchElementException
是一个RuntimeException
,可以由Java中的不同类别抛出
像Iterator,Enumerator, Scanner
或StringTokenizer。
在你的情况下,它是扫描器
。它不是从格式
方法。
这是只是为了更安全如果没有给出下一个输入,那么抛出这个异常)。
演示演示的示例代码
public class NoSuchElementExceptionDemo {
/ pre>
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)
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)
inFormatter
class throws twoexception
:IllegalFormatException
andFormatterClosedException
but in my book the above code catchesNoSuchElementException
andFormatterClosedException
.
- Why did the code catch
NoSuchElementException
but did not catchIllegalFormatException
?- 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 aRuntimeException
which can be thrown by different classes in Java like Iterator, Enumerator, Scanner or StringTokenizer.In your case it is
Scanner
. Its not fromformat
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屋!