Why I m getting Unreported Exception

后端 未结 2 1686
[愿得一人]
[愿得一人] 2021-01-29 15:33

can you please why error comes in line 13 as unreported exception ,must be caught pr declared to be thrown

class Demo {
    public static void main(String args[]         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 15:39

    At the last line of your code you are throwing an exception but there is nothing handling it. You must do one of two:

    1. Surround it with a try/catch block
    2. Use the throws keyword in the method's signature. See this SO question: The throws keyword for exceptions in Java

    In addition this question: Why is “throws Exception” necessary when calling a function?


    Secondly after adding it the code will compile but when executed will still throw an exception:

    Exception in thread "main" java.lang.ArithmeticException: / by zero

    Reason is that in the catch blocks you are re-throwing the exception.

提交回复
热议问题