0

We all know that if you hit a Governor Limit Exception, your code/application will terminate with an unhandleable exception know as a Governor Limit Exception.

My question is does your code terminated at the point at which the limit is exceeded? If so, even if you have a finally block, it will not get run since your code has terminated?

2

When you hit a LimitException, it terminates the entire transaction. No other Apex code you have written will run within the same transaction, because it is over.

2

@M guy I Have gone through your doubt and found this:-

I have a class as follows;-

public class testGoverner {
Public static void insertcontact(){
    try{
        for(Integer i=0;i<1500;i++){
            Contact c = new Contact();
            c.LastName='system'+i;
            insert c;
        }
    }catch(DmlException e){
        system.debug(e.getMessage());
    }
    finally{
        system.debug('Hit the governer limit');
    }
}

} When I run this class using execute annonymous window, I got error System.LimitException: Too many SOQL queries: 101

So, I tried calling this in another class as follows:-

public class TestClas {
public static void test(){
    try{
    system.debug('check');
    testGoverner.insertcontact();
    }finally{
        system.debug('hit governer');
    }
}

}

And again run it, so again I got the same error System.LimitException: Too many SOQL queries: 101

So in both the cases, it gets terminated without going to finally block and no debugs log got generated.

then I tried calling this class with Visualforce page and execute it on click of button as follows:-

<apex:page controller="testGoverner">
<apex:form>
    <apex:pageBlock>
    <apex:pageBlockButtons>
        <apex:commandButton action="{!insertcontact}" value="check"/>
    </apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>

And Got this:- Too many DML statements: 151 Error is in expression '{!insertcontact}' in component in page testgovern: Class.testGoverner.insertcontact: line 7, column 1

This time debug log get generated but finally statement does not get executed. Note:- even catch block does not get executed.

So I can say you are correct that your code terminated at the point at which the limit is exceeded. Even if you have a finally block, it will not get run since your code has terminated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.