class throwseg1
{
void show() throws Exception
{
throw new Exception("my.own.Exception");
}
void show2() throws Exception // Why throws is necessary here ?
{
show();
}
void show3() throws Exception // Why throws is necessary here ?
{
show2();
}
public static void main(String s[]) throws Exception // Why throws is necessary here ?
{
throwseg1 o1 = new throwseg1();
o1.show3();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器报告方法show2(),show3()以及main()具有
未报告的异常必须捕获或声明要抛出的异常
当我throws Exception从这些方法中删除?
java exception-handling unhandled-exception throws checked-exceptions
我有一个例外发生异常的功能
private void functionName() throws Exception
{
// some code that might throw an exception
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
所以我认为我对Java中的异常处理有一个很好的基本理解,但我最近阅读的一些代码让我有些困惑和怀疑.我想在这里解决的主要疑问是,一个人何时应该使用Java方法声明,如下所示:
public void method() throws SomeException
{
// method body here
}
Run Code Online (Sandbox Code Playgroud)
通过阅读一些类似的帖子,我收集到throws用作一种声明,在执行方法期间可能抛出SomeException.
我的困惑来自一些看起来像这样的代码:
public void method() throws IOException
{
try
{
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}Run Code Online (Sandbox Code Playgroud)
你有什么理由想在这个例子中使用投掷吗?看来如果你只是在做IOException之类的基本异常处理,你只需要try/catch块就可以了.
在决定是否向方法添加throws子句或使用try-catch?时,一般的经验法则是什么?
从我自己阅读的内容来看,throws应该在调用者违反合同结束时使用(传递对象),并且try-catch在方法内执行操作期间发生异常时应该使用.它是否正确?如果是这样,应该在呼叫者方面做些什么?
PS:通过谷歌和搜索引擎优化搜索,但希望得到一个明确的答案.
寻找一些参考资料后弄明白,-unfortunately-我找不到任何关于理解之间的差异有用-和简单-描述throws和rethrows.当试图理解我们应该如何使用它时,这有点令人困惑.
我想提一下,我对-default-熟悉throws传播错误的最简单形式,如下所示:
enum CustomError: Error {
case potato
case tomato
}
func throwCustomError(_ string: String) throws {
if string.lowercased().trimmingCharacters(in: .whitespaces) == "potato" {
throw CustomError.potato
}
if string.lowercased().trimmingCharacters(in: .whitespaces) == "tomato" {
throw CustomError.tomato
}
}
do {
try throwCustomError("potato")
} catch let error as CustomError {
switch error {
case .potato:
print("potatos catched") // potatos catched
case .tomato:
print("tomato catched")
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一直很好,但问题出现在:
func throwCustomError(function:(String) throws -> ()) throws {
try function("throws string") …Run Code Online (Sandbox Code Playgroud) 可以任你解释的区别是什么之间throw,throws以及Throwable和什么时候使用?
我可以在php中声明一个抛出异常的函数吗?例如:
public function read($b, $off, $len) throws IOException
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚方法签名中的Throws和Java中的Throw Statements之间的区别.方法签名中的引发如下:
public void aMethod() throws IOException{
FileReader f = new FileReader("notExist.txt");
}
Run Code Online (Sandbox Code Playgroud)
抛出语句如下:
public void bMethod() {
throw new IOException();
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,throwsin方法签名是一种通知,该方法可能会抛出这样的异常.throw语句是在相应的情况下实际抛出创建的对象.从这个意义上讲,如果方法中存在throw语句,则应始终显示方法签名中的throws.
但是,以下代码似乎没有这样做.代码来自库.我的问题是它为什么会发生?我理解错误的概念吗?
这段代码是java.util.linkedList的副本.@author Josh Bloch
/**
* Returns the first element in this list.
*
* @return the first element in this list
* @throws NoSuchElementException if this list is empty
*/
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw …Run Code Online (Sandbox Code Playgroud)