function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hramanihramani 

Capturing an exception in a catch block from another method salesforce

I have method , method1() with a try catch block. I also have 2 more methods, method2() and method3() which does some operation and throws an exception.
I want to catch that exception and perform my operation based on the exception message. Below is the code.
I’m getting error here. Please help.

public void static method1(){

try{

Calls method

}catch(Exception ex){

    if (ex == ‘Configuration JSON value limit exceeded’){
        //DO AN OPERATION AND THROW A CUSTOM EXCEPTION
    }
    else if(‘RuleAction HTMLContent limit exceeded’){
        //DO A DIFFERENT OPERATION AND THROW A CUSTOM EXCEPTION
    }

}

}


public void static method2(){

If (//CONDITION SATISFIES){
Calls method3();
}
Else{
throw new StringException('Configuration JSON value limit exceeded');
}
}

public void static method3(){

If (//CONDITION SATISFIES){
Perform an operation
}
else
{
throw new StringException('RuleAction HTMLContent limit exceeded');
}
}
Best Answer chosen by hramani
Navin Selvaraj23Navin Selvaraj23
Hi,

PFB the sameple code. It will be working fine.
public class ExceptionTest {
    
    public  static void method1(Integer anyNumber){
        
        try{
            
            method2(anyNumber);
            
        }catch(Exception ex){
            
            if (ex.getMessage() == 'Configuration JSON value limit exceeded'){
                System.debug('Configuration JSON value limit exceeded');
            }
            else if(ex.getMessage() == 'RuleAction HTMLContent limit exceeded'){
                System.debug('RuleAction HTMLContent limit exceeded');
            }
            
        }
        
    }
    
    
    public  static void method2(Integer anyNumber){
        
        If (anyNumber > 5){
            method3(anyNumber);
        }
        Else{
            throw new StringException('Configuration JSON value limit exceeded');
        }
    }
    
    public static void method3(Integer anyNumber){
        
        If (anyNumber > 10){
            System.debug('Success');
        }
        else
        {
            throw new StringException('RuleAction HTMLContent limit exceeded');
        }
    }
}

you can test above code in anonymous window as like below
ExceptionTest.method1(7);

Also, Here some useful links.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_custom.htm


Hope it helps. If it helps, mark it as best answer and mark it as solved. it heps out community clean,

Regards,

Navin

All Answers

Navin Selvaraj23Navin Selvaraj23
Hi,

PFB the sameple code. It will be working fine.
public class ExceptionTest {
    
    public  static void method1(Integer anyNumber){
        
        try{
            
            method2(anyNumber);
            
        }catch(Exception ex){
            
            if (ex.getMessage() == 'Configuration JSON value limit exceeded'){
                System.debug('Configuration JSON value limit exceeded');
            }
            else if(ex.getMessage() == 'RuleAction HTMLContent limit exceeded'){
                System.debug('RuleAction HTMLContent limit exceeded');
            }
            
        }
        
    }
    
    
    public  static void method2(Integer anyNumber){
        
        If (anyNumber > 5){
            method3(anyNumber);
        }
        Else{
            throw new StringException('Configuration JSON value limit exceeded');
        }
    }
    
    public static void method3(Integer anyNumber){
        
        If (anyNumber > 10){
            System.debug('Success');
        }
        else
        {
            throw new StringException('RuleAction HTMLContent limit exceeded');
        }
    }
}

you can test above code in anonymous window as like below
ExceptionTest.method1(7);

Also, Here some useful links.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_custom.htm


Hope it helps. If it helps, mark it as best answer and mark it as solved. it heps out community clean,

Regards,

Navin
This was selected as the best answer
hramanihramani
Thanks Navin