Why “not all control paths return a value” is warning and not an error?

后端 未结 7 1823
花落未央
花落未央 2020-12-02 01:48

I was trying to answer this question. As suggested by the accepted answer, the problem with that code is that not all control paths are returning a value. I tried this code

7条回答
  •  感情败类
    2020-12-02 02:05

    here is another reason it isn't an error

    the following will give you the same warning since the compiler expects you to return something from the catch block even though you're throwing there

    int foo(){
       try{
          return bar(0);
       } catch(std::exception& ex){
          //do cleanup
          throw ex;
       }
    }
    
    int bar(unsigned int i){
       if(i == 0){
          throw std::string("Value must be greater than 0");
       } else{
          return 0;
       }
    }
    

提交回复
热议问题