top of page

When a caught exception is thrown again?

It is primarily useful when the method is called by another method and the exception handling is transferred to the caller method.

Eg:

create a custom exception named MerchandiseException

public class MerchandiseException extends Exception { }

create a second class named MerchandiseUtility

public class MerchandiseUtility {

//the caller method is mainProcessing()

//the called method is insertMerchandise() public static void mainProcessing() { try { insertMerchandise(); } catch(MerchandiseException me) { System.debug('Message: ' + me.getMessage()); System.debug('Cause: ' + me.getCause()); System.debug('Line number: ' + me.getLineNumber()); System.debug('Stack trace: ' + me.getStackTraceString()); } } //when this method is called it would cause an exception

//the catch block catches this exception

//and throws the custom exception - MerchandiseException

//with the first Dmlexception variable as the inner exception

//finally it will be handled by the caller method - mainProcessing() public static void insertMerchandise() { try { // Insert merchandise without required fields Merchandise__c m = new Merchandise__c(); insert m; } catch(DmlException e) { throw new MerchandiseException( 'Merchandise item could not be inserted.', e); } } }

Recent Posts

See All

Comments


Post: Blog2_Post

©2020 by SalesforceDemystified. Proudly created with Wix.com

bottom of page