Why is “throws Exception” necessary when calling a function?

后端 未结 8 1696
借酒劲吻你
借酒劲吻你 2020-11-29 15:37
class throwseg1
{
    void show() throws Exception
    {
        throw new Exception(\"my.own.Exception\");
    }

    void show2() throws Exception  // Why throws i         


        
相关标签:
8条回答
  • 2020-11-29 15:53
    void show() throws Exception
    {
        throw new Exception("my.own.Exception");
    }
    

    As there is checked exception in show() method , which is not being handled in that method so we use throws keyword for propagating the Exception.

    void show2() throws Exception //Why throws is necessary here ?
    {
    show();
    }
    

    Since you are using the show() method in show2() method and you have propagated the exception atleast you should be handling here. If you are not handling the Exception here , then you are using throws keyword. So that is the reason for using throws keyword at the method signature.

    0 讨论(0)
  • 2020-11-29 15:53

    If you propagate the exception by declaring the throws directive in the signature of the current method, then somewhere up the line or call stack a try/catch construct must be used to handle the exception.

    0 讨论(0)
提交回复
热议问题