Stop executing further code in Java

后端 未结 6 2047
失恋的感觉
失恋的感觉 2021-01-01 16:25

I have looked in the Javadoc but couldn\'t find information related to this.

I want the application to stop executing a method if code in that method tells it to do

6条回答
  •  不知归路
    2021-01-01 17:13

    There are two way to stop current method/process :

    1. Throwing Exception.
    2. returnning the value even if it is void method.

    Option : you can also kill the current thread to stop it.

    For example :

    public void onClick(){
    
        if(condition == true){
            return;
            
            throw new YourException();
        }
        string.setText("This string should not change if condition = true");
    }
    

提交回复
热议问题