标签: throws

调用函数时为什么需要"抛出异常"?

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

90
推荐指数
3
解决办法
18万
查看次数

在Java中,C#中是否存在throws关键字?

可能重复:
如何在C#中使用Java风格的throws关键字?

我有一个例外发生异常的功能

private void functionName() throws Exception
{
   // some code that might throw an exception
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

c# java exception throw throws

85
推荐指数
3
解决办法
7万
查看次数

如何在C#中使用Java风格的throws关键字?

在Java中,throws关键字允许方法声明它不会自己处理异常,而是将其抛给调用方法.

C#中是否有类似的关键字/属性?

如果没有等效物,你怎么能达到相同(或相似)的效果呢?

c# java exception-handling throws

82
推荐指数
5
解决办法
5万
查看次数

何时在Java方法声明中使用throws?

所以我认为我对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块就可以了.

java exception-handling throws

82
推荐指数
3
解决办法
12万
查看次数

投掷或尝试捕获

在决定是否向方法添加throws子句或使用try-catch?时,一般的经验法则是什么?

从我自己阅读的内容来看,throws应该在调用者违反合同结束时使用(传递对象),并且try-catch在方法内执行操作期间发生异常时应该使用.它是否正确?如果是这样,应该在呼叫者方面做些什么?

PS:通过谷歌和搜索引擎优化搜索,但希望得到一个明确的答案.

java exception-handling exception try-catch throws

71
推荐指数
4
解决办法
5万
查看次数

有没有办法让Runnable的run()抛出异常?

我在实现Runnable的类中调用run()的方法被设计为抛出异常.

但Java编译器不会让我这样做,并建议我用try/catch包围它.

问题是,通过try/catch包围它,我使特定的 run()无用.我确实想抛出那个例外.

如果我throwsrun()本身指定,编译器会抱怨Exception is not compatible with throws clause in Runnable.run().

通常我完全没有让 run()抛出异常.但我有独特的情况,我必须具备该功能.

如何解决这个限制?

java android runnable throws

67
推荐指数
6
解决办法
6万
查看次数

Swift中的throws和rethrows有什么区别?

寻找一些参考资料后弄明白,-unfortunately-我找不到任何关于理解之间的差异有用-和简单-描述throwsrethrows.当试图理解我们应该如何使用它时,这有点令人困惑.

我想提一下,我对-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)

error-handling try-catch rethrow throws swift

64
推荐指数
2
解决办法
1万
查看次数

异常处理:抛出,抛出和Throwable

可以任你解释的区别是什么之间throw,throws以及Throwable和什么时候使用?

java exception-handling throw throwable throws

54
推荐指数
3
解决办法
5万
查看次数

我可以声明一个php函数抛出异常吗?

我可以在php中声明一个抛出异常的函数吗?例如:

public function read($b, $off, $len) throws IOException 
Run Code Online (Sandbox Code Playgroud)

php exception throw throws

38
推荐指数
3
解决办法
3万
查看次数

方法签名中的抛出与Java中的抛出语句之间的区别

我试图弄清楚方法签名中的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)

java throw throws

33
推荐指数
2
解决办法
3万
查看次数