Try-Catch behavior in Apex

When working with complex projects using try-catch in apex and storing the caught exceptions into the log for further troubleshooting is the key to improve project quality.

The below example will help you understand how execution will take steps.
---------Example:1---------
public class TestBatchClass implements Database.Batchable<sObject> {
	
    public Database.QueryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator([SELECT Name FROM User WHERE IsActive = true LIMIT 10]); 
    }
    
    //Execute method
    public void execute (Database.BatchableContext bc, List<User> lstUser)
    {
        try
        {
            Integer a=10/0;
        }
        catch(Exception exe)
        {
            System.debug('1 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName());
        }
        try
        {
            Integer a=10/0;
        }
        catch(Exception exe)
        {
            System.debug('2 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName());
        }
    }
    
    public void finish (Database.BatchableContext bc)
    {

    }
}

---------Example:2---------
trigger TestAccTrig on Account (before insert,before update)
{    
    execute();
    public void execute ()
    {
        try
        {
            Integer a=10/0;
        }
        catch(Exception exe)
        {
            System.debug('1 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName());
        }
        try
        {
            Integer a=10/0;
        }
        catch(Exception exe)
        {
            System.debug('2 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName());
        }

        System.debug('3 Final statement ');                
    }
}
---------Example:3---------

trigger TestAccTrig on Account (before insert,before update) { execute(); public void execute () { try { Integer a=10/0; } catch(Exception exe) { System.debug('1 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName()); } try { Integer a=10/0; } catch(Exception exe) { System.debug('2 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName()); } try { TestAccTrigHanlder.m1(); } catch(Exception exe) { System.debug('--Final statement '); } } } public class TestAccTrigHanlder { public static void m1() { Integer a=10/0; } }
---------Example:4---------
trigger TestAccTrig on Account (before insert,before update) { execute(); public void execute () { try { Integer a=10/0; } catch(Exception exe) { System.debug('1 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName()); } try { Integer a=10/0; } catch(Exception exe) { System.debug('2 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName()); } TestAccTrigHanlder.m1(); System.debug('3 Final statement '); } }

output
Try on your own by executing the above examples.

Note: 
 try
 {
    Integer a=10/0;
    System.debug('This line will never execute because the above line caused the exception
 will move the cursor to catch block. Again cursor will not returned to the exception caused 
try block');
 }
 catch(Exception exe)
 {
   System.debug('2 getCause '+exe.getCause() + 'getMessage ' + exe.getMessage() + 'getTypeName '+ exe.getTypeName()); 
 }

Comments