Mytutorialrack

I always had this bad feeling, that I wasn’t following the best practices handling exceptions in Apex. In most of the cases, I wasn’t doing any exception handling.

What is an exception?

An exception is a condition which changes the normal flow of program execution.That is when something bad happens which the program can’t handle during execution.Exception are the apex way of throwing up its hands and saying “I can not deal with this, you need to handle it”
What are conditions which result into exception?

  • Your code expects a value from something which is currently Null.
  • An insert or update falis because of the failure of custom validation rule which you have set up.
  • Assigning a query which returns no records or more than one record to a singleton sObject variable.
  • Accessing a list index which is out of bounds.

In all the above situations we are trying something which apex consider it impossible and an exception is thrown. Apex has 20 different types of exceptions.But all these exceptions are subclassed from a generic exception class.All the exception support standard methods for accessing the error message and the exception type.
What happens when an exception ocurrs?
When an exception occurs and you haven’t written the code to deal with it, it is called ‘unhandled’. An unhandled exception brings processing to a halt. If the code
which has processed so far contained any DML statements (Database Manipulation language), those statements will be rolled back completely.
When the exception occurs, the system notifies the running User of the problem. If you run into an exception in the apex code while using the standard salesforce UI, a red text message will appear on top of your screen displaying the text of the unhandled exception.
If the exception occurred due to the failure of custom field validation error and you’ve selected ‘next to the field’, then the exception message will appear next to the field in red color.
The system will notify the developer of the code in question that there has been a unhandled exception. An email will be sent to the developer with the Organization Id and User ID of the running user, as well as the exception message.
How do I catch exceptions?
Apex allows you tp handle your exception and write code to recover from an error. Apex uses try, other programming languagecother programming languages). You write your code inside the “try” block and if an exception occurred then you “catch” it and can run some code and “finally” you run some code whether you had an exception or not.
You have multiple catch blocks to catch any of the 20 different kinds of exception.

try{
     //Your code here
} catch (ListException e) {
     //Optional catch of a specific exception type
     //Specific exception handling code here
} catch (Exception e) {
     //Generic exception handling code here
} finally {
     //optional finally block
     //code to run whether there is an exception or not
}

An exception can occur in DML statements. For example, if you try to insert a record without providing the value for a required field. The addError() method can be called on a record or field and it will prevent the DML operation from committing.
For example:

 try{
     update accounts;
} catch (DMLException e){
     for (Account account : accounts) {
          account.addError('There was a problem updating the accounts');
     }
} finally {
     inProgress = false;
}

Visualforce:
If you have a custom controller or controller extension for a visualforce page you can handle exceptions just as in the example above.
To show the error on visualforce page, you can use ApexPages.message class to create a message for display.

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'my error msg');
ApexPages.addMessage(myMsg);

Summary:
Exception handling is a very important part of your application, and often it is overlooked. The good news is Apex makes it very easy to handle your own exceptions. Use try-catch-finally statements to catch system exceptions as they occur. Use custom exceptions to throw your own exception and adapt even when the system doesn’t object to program flow. These techniques will give better user experience.
You can keep the user aware of any errors you run into via Apex page messages in Visualforce and object .addError() messages in Apex triggers. if you want to be notified as the developer, you can write Apex email notifications or write your error to a custom object so you know when your code is misbehaving.
Apex has a robust framework for exception handling, start using it today in your triggers and classes.
 

Share:

Recent Posts