How to exit an apex function?
Asked Answered
R

2

8

I have an apex function which has a void return type. I want to exit the function at a specific position. Is this possible in apex without changing return type pagereference?

Recusancy answered 13/2, 2013 at 6:11 Comment(0)
W
18

In apex, the return keyword signals to immediately stop processing statements in that function.

public void doNothing() {
     return;
}
Wherewithal answered 13/2, 2013 at 7:58 Comment(0)
F
0

I'm not sure that I correctly understood your question (more detailed description will be useful), but it seems that you can use a return statement by the following way:

public PageReference method() {
    //some your code ....
    if (some specific condition) { 
        return null;
    }

    PageReference pageRef = new PageReference('someURL');
    return pageRef;
}

Please show your code for a more target help

Flattop answered 13/2, 2013 at 6:48 Comment(1)
thanks for quick reply. If we have return type for method as pagereference the 'return null' will work for exit the method. But if we have method that have return type as 'void' then, how to exit from that method? Example: public void method() { //some your code .... if (some specific condition) { //how to exit } }Recusancy

© 2022 - 2024 — McMap. All rights reserved.