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

后端 未结 8 1767
借酒劲吻你
借酒劲吻你 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.

提交回复
热议问题