Return statements in catch blocks

后端 未结 6 1534
渐次进展
渐次进展 2021-01-13 05:29

I have seen some developers use the return statement in a catch block. Why/when would this be a useful technique to employ?

EDIT: I actually just saw the return keyw

6条回答
  •  温柔的废话
    2021-01-13 06:27

    public void Function() {

    try 
    { 
        //some code here
    }
    catch
    { 
        return;
    }
    

    }

    when return; is hit, the execution flow jumps out of the function. This can only be done on void methods.

    EDIT: you do this if you dont want to execute the rest of the function. For example if you are doing file IO and a read error happens, you dont want to execute code that handles processing the data in that file since you dont have it.

提交回复
热议问题