I have an custom exception , just an example to see how I can write my own exception class and what could be the purpose of the exception.
public class AccountException extends Exception {
public AccountException( Exception e, string sourceClass){
system.debug('Exeption Name ******' + e);
}
}
Below is the code where I use the exception:
public class accontecreation {
public void createtheAcc(){
try{
Account a = new Account();
insert a;
}
Catch(Exception e){
throw new AccountException(e,'lkansldknas');
}
}
}
When I call the above class method using Execute Anonymous
, I get an System.DMLException
right away instead of showing up in the debug log. What am I doing wrong here? How can I catch various kind of exception and handle it using my own custom exception class? Is there any correction I can make to get this to go to catch block and show up the error in system.debug?
AccountException
SF has no clue what that is and will never throw it. If you make a custom exception that doesn't mean the platform will throw it, that means you have to throw it.