Salesforce Developer Interview Questions – Apex debugging and Exception handling
APEX- Debugging and Exception handling
Table of Contents
54. What is debugging and How do you debug apex ?
Answer:
Debugging can be defined as the process of finding the root cause of a problem or a bug in a code and fixing it.
Salesforce provides debug logs to debug apex . We can also access debug logs from the developer console.
55. Can you explain the steps to debug the code in apex?
Answer:
Step 1 : Enable the debug logs . To enable the debug logs goto setup → Quick find Debug logs →
Click New “Select the users for which you want to enable debug logs”
Enter “Start Date”
Enter “Expiration Date”
Enter “Debug Level “
Step 2 : Run the functionality for which you want to debug .
Step 3 : Go back to the debug logs from setup and Open the debug logs for that particular user.
Step 4 : Analyze the debug logs to find the issue.
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/55%20Steps%20to%20debug%20code163cfbc3c28eac_lg.png)
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/55%20Steps%20to%20debug%20code263cfbc497e4c7_lg.png)
56. What does the debug log file contain or What information does the debug log provide?
Answer:
Debug log provides the information about the following.
- Database changes
- HTTP callouts
- Apex errors
- Resources used by Apex
Automated workflow processes, such as:
- Workflow rules
- Assignment rules
- Approval processes
- Validation rules
- Process builder
- FLOWS
Note : The debug log does not include information from actions triggered by time-based workflows.
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/56%20Sample%20Debug%20logs%20163cfbc6958864_lg.png)
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/56%20Debug%20logs%20in%20developer%20console%20263cfbc789bcf9_lg.png)
57. Why do we use system.debug()?
Answer:
- System.debug is used to print information of a specific line of code in apex or triggers.
- Apart from the general debug information, debug logs contains the information from system.debug statements.
Ex: If we want to see if a particular list or set is updated with right values after the logic is processed then we write system.debug to see that list
System.debug(‘accList ⇒’ + accList);
58. What is an exception and what happens when an exception occurs?
Answer:
Exception is an error or event that disrupts the normal flow of code execution. When an exception occurs , code execution halts.
59. What are exception statements?
Answer:
There are two exception statements:
- Throw statements : Used to generate exception
- Try, catch, and finally : Used to gracefully recover from an exception.
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/59%20Exception%20Statement%20163cfbcaa999b2_lg.png)
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/59%20Exception%20Statement%20263cfbcb9782fc_lg.png)
60. Name a few built-in exception types?
Answer:
- DMLExcption
- ListExcption
- NullPointerException
- QueryException
- SObjectException
61. Provide an example of handling generic exceptions as well as specific exceptions using built-in exception types?
Answer:
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/61%20Built%20in%20exception%20types63cfbcc9e5127_lg.png)
62. Can you write sample code for a custom exception?
Answer:
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/62%20Custom%20Exceptions63cfbcd8a0bad_lg.png)
63. Can we handle governors limit exceptions using try catch ?
Answer:
- NO.
- When we encounter governor limits, code executions halts immediately and this cannot be handled using exception handling .