Throwing multiple exceptions in Java

后端 未结 11 1491
无人及你
无人及你 2020-12-28 13:04

Is there any way to throw multiple exceptions in java?

11条回答
  •  心在旅途
    2020-12-28 14:03

    A method can throw one of several exceptions. Eg:

     public void dosomething() throws IOException, AWTException {
          // ....
     }
    

    This signals that the method can eventually throw one of those two exceptions (and also any of the unchecked exceptions). You cannnot (in Java or in any language AFAIK) throw simultaneously two exceptions, that would not make much sense.

    You can also throw a nested Exception, which contains inside another one exception object. But that would hardly count that as "throwing two exceptions", it just represents a single exception case described by two exceptions objects (frequently from different layers).

提交回复
热议问题