Throwing multiple exceptions in Java

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

Is there any way to throw multiple exceptions in java?

11条回答
  •  天命终不由人
    2020-12-28 13:44

    there is a way for a method to throw mutiple exeptions, but not at one time. e.g. when compilation fails for some reason your method can only throw one exception. if you have to cover different opportunities you might declare you method to throw the parent class of all exceptions "Exception". so, if you declare a method to throw an Exception in general, an exception of any type can be thrown by this method.

    for example:

    public static void main(String[] args) throws Exception
    {
      getClipboard();    // throws an UnsupportedFlavorException
      initIOActivity();  // throw an IOException
    }
    

    i don't know what you actually needed to know but maybe this helps. although it has passed a lot of time since your post ^^

    greetings

提交回复
热议问题