{"id":1551,"date":"2026-09-07T15:00:00","date_gmt":"2026-09-07T09:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/"},"modified":"2026-09-07T15:00:00","modified_gmt":"2026-09-07T09:30:00","slug":"apex-exception-handling-custom-exceptions","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/","title":{"rendered":"Apex Exception Handling and Custom Exceptions Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Exception Handling is how your code reacts when something goes wrong, such as a missing record, a failed save or a bad value. Instead of showing users a long technical error, good code catches the problem, logs it and shows a clear message. It also cleans up, so data stays consistent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn try, catch and finally, the built-in exceptions you meet most, how to create custom exceptions, and how to show errors to users from triggers and Lightning components. This is the last topic of Level 4. It builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/dml-operations-in-apex\/\">DML operations in Apex<\/a>, <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-savepoints-and-rollback\/\">Apex savepoints and rollback<\/a> and the <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-order-of-execution-developers\/\">Apex order of execution<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Apex Exception Handling?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An exception is an error that stops the normal flow of code, such as dividing by zero or saving a record with a missing required field. Apex Exception Handling uses try, catch and finally blocks to respond to these errors. You can also create your own exception classes for business rules, such as &#8220;course is full&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a train driver. When a signal turns red, the driver does not crash the train. He stops safely, informs the control room and waits for instructions. Apex Exception Handling is that safety procedure for your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Exception Handling Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unhandled errors confuse users and leave half-finished work. Here is why Apex Exception Handling matters at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, an enrolment page should say &#8220;This course is full&#8221;, not show a stack trace.<\/li>\n\n<li>Also, a nightly import should log failed rows and continue.<\/li>\n\n<li>Next, a payment step must roll back the enrolment if it fails.<\/li>\n\n<li>Support teams need error details to fix problems quickly.<\/li>\n\n<li>Finally, some errors, such as governor limit errors, cannot be caught at all.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So careful handling keeps users informed and data correct.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Exception Handling Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Exception Handling follows a clear flow. First, code that might fail runs inside a try block. Next, if an exception is thrown, Apex jumps to the matching catch block. Then the catch block logs the error, rolls back if needed, and shows or rethrows a clear message. After that, the finally block runs, whether there was an error or not. Finally, the code continues, or the exception moves up to the caller.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-1-1024x640.png\" alt=\"Apex Exception Handling diagram showing try, throw, catch, handle, finally and continue steps\" class=\"wp-image-1549\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">How Apex moves from try to catch to finally.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">try, catch and finally<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The try block holds risky code. A catch block handles one exception type, and you can have several. The finally block always runs, which is useful for cleanup or logging.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Built-In Exceptions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Apex has many built-in exception types, such as DmlException, QueryException and NullPointerException. Catch the specific type when you can, and use the general Exception type last.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Custom Exceptions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You create a custom exception by extending the Exception class. Its name must end with Exception, such as CourseFullException.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exception Methods<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every exception has useful methods. getMessage returns the text, getTypeName returns the class name, getLineNumber returns the line, and getStackTraceString shows where it came from.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">LimitException<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A LimitException, such as too many queries, cannot be caught. So the transaction always fails, and you must prevent these errors with good code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Built-In Exceptions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the exceptions you will meet most often.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Exception<\/th><th>Common cause<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>DmlException<\/td><td>A save fails validation or a required field<\/td><td>insert with a missing Name<\/td><\/tr><tr><td>QueryException<\/td><td>Query assigned to one record finds none<\/td><td>Course__c c = [SELECT &#8230;] with no rows<\/td><\/tr><tr><td>NullPointerException<\/td><td>Using a variable that is null<\/td><td>c.Fee__c when c is null<\/td><\/tr><tr><td>ListException<\/td><td>Index outside the list<\/td><td>rows[5] on a list of 3<\/td><\/tr><tr><td>SObjectException<\/td><td>Reading a field that was not queried<\/td><td>c.Level__c not in SELECT<\/td><\/tr><tr><td>MathException<\/td><td>Dividing by zero<\/td><td>total \/ 0<\/td><\/tr><tr><td>LimitException<\/td><td>Governor limit passed, cannot be caught<\/td><td>101 SOQL queries<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">addError vs Throwing an Exception<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares two ways to stop a save from a trigger.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>record.addError<\/th><th>Throwing an exception<\/th><\/tr><\/thead><tbody><tr><td>Stops<\/td><td>Only the marked records<\/td><td>The whole transaction<\/td><\/tr><tr><td>Message to the user<\/td><td>Clean, custom text<\/td><td>Often technical<\/td><\/tr><tr><td>Other records in the batch<\/td><td>Can still save with partial success<\/td><td>All fail<\/td><\/tr><tr><td>Best for<\/td><td>Business validation in triggers<\/td><td>Unexpected errors in classes<\/td><\/tr><tr><td>Works in<\/td><td>Trigger context<\/td><td>Any Apex<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is simple. Use addError for validation in triggers. Throw exceptions in service classes for unexpected problems, and let callers decide how to handle them.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-2-1024x640.png\" alt=\"Apex Exception Handling diagram comparing addError and throwing an exception\" class=\"wp-image-1550\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">addError vs throwing an exception.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Exception Handling Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s add Apex Exception Handling to the Bright Path Tutors enrolment service.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a Custom Exception<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CourseFullException extends Exception {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That one line is a complete custom exception. You can throw it with a message, such as <code>throw new CourseFullException('Apex Basics is full')<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Use It in the Service<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class EnrolmentService {\n\n    public static Id enrol(Id courseId, Id studentId) {\n        Course__c c = [SELECT Id, Name, Seats_Booked__c, Max_Seats__c\n                       FROM Course__c WHERE Id = :courseId];\n\n        if (c.Seats_Booked__c != null &amp;&amp; c.Max_Seats__c != null\n                &amp;&amp; c.Seats_Booked__c &gt;= c.Max_Seats__c) {\n            throw new CourseFullException(c.Name + ' is full. Please choose another batch.');\n        }\n\n        Enrolment__c e = new Enrolment__c(Course__c = courseId, Student__c = studentId,\n                                          Status__c = 'Active');\n        insert e;\n        return e.Id;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method checks the seat count first. If the course is full, it throws CourseFullException with a clear message. Otherwise, it saves the enrolment as usual.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Handle Errors for a Lightning Component<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class EnrolmentController {\n\n    @AuraEnabled\n    public static Id enrolStudent(Id courseId, Id studentId) {\n        try {\n            return EnrolmentService.enrol(courseId, studentId);\n        } catch (CourseFullException e) {\n            throw new AuraHandledException(e.getMessage());\n        } catch (DmlException e) {\n            ErrorLogger.log('EnrolmentController', e);\n            throw new AuraHandledException(e.getDmlMessage(0));\n        } catch (Exception e) {\n            ErrorLogger.log('EnrolmentController', e);\n            throw new AuraHandledException('Something went wrong. Please contact support.');\n        } finally {\n            System.debug('enrolStudent finished for course ' + courseId);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Catch blocks go from the most specific to the most general. The custom exception passes its friendly message to the user through AuraHandledException. DML and unexpected errors are logged first, then shown with a safe message. The finally block always runs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: A Simple Error Logger<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public without sharing class ErrorLogger {\n    public static void log(String source, Exception e) {\n        System.debug(LoggingLevel.ERROR, source + ': ' + e.getTypeName() + ' - '\n            + e.getMessage() + ' at line ' + e.getLineNumber());\n        \/\/ In a real org, insert an Error_Log__c record or publish a platform event\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The logger prints the type, message and line number. In a real org, you would save these details in a custom log object so support teams can review them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Read the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is what the log shows when a full course is chosen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [21]|DEBUG|enrolStudent finished for course a015g00000AbC12AAB<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The user sees &#8220;Apex Basics is full. Please choose another batch.&#8221; on the screen, while the finally block still logs that the method finished.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Exception Handling in Triggers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In triggers, prefer addError for business rules. It blocks only the bad records and shows a clean message. If a trigger throws an unhandled exception, the whole save fails, and users may see a technical error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Logging Errors Safely<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An error log record inserted in the same transaction is rolled back if the transaction fails. So for serious failures, many teams publish a platform event set to publish immediately, or log from async code. We will cover platform events in the async level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Exception Handling<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Catching Exception and doing nothing, which hides real bugs.<\/li>\n\n<li>Showing raw technical messages to users.<\/li>\n\n<li>Trying to catch LimitException, which is not possible.<\/li>\n\n<li>Putting the general catch block before specific ones.<\/li>\n\n<li>Logging errors with DML that rolls back with the failed transaction.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Exception Handling<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Catch specific exceptions first and Exception last.<\/li>\n\n<li>Use custom exceptions for clear business errors.<\/li>\n\n<li>Use addError in triggers and AuraHandledException for Lightning components.<\/li>\n\n<li>Log type, message, line number and context for every unexpected error.<\/li>\n\n<li>Prevent limit errors with bulk code, because they cannot be caught.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a custom exception called InvalidFeeException. Throw it from a service method when a course fee is negative, and catch it in a controller that returns a friendly message. Hint: check <code>Fee__c &lt; 0<\/code> before the update.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on Apex Exception Handling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Exception Handling questions test whether you write production-ready code. Here are the 21 questions asked most about Apex Exception Handling.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on Apex Exception Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is an exception in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is an error that stops the normal flow of code, such as a failed save or a null value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: What are try, catch and finally?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">try holds risky code, catch handles errors, and finally always runs afterwards.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: How do you create a custom exception?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a class that extends Exception. Its name must end with Exception.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: Name some built-in exceptions.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, DmlException, QueryException, NullPointerException, ListException, SObjectException and MathException.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Can you catch a LimitException?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Governor limit errors cannot be caught, so the transaction fails.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: What does getMessage return?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The error text of the exception.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: When does the finally block run?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Always, whether an exception happened or not.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on Apex Exception Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: What causes a QueryException?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Often, assigning a query to a single record when no rows or several rows match.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: In what order should catch blocks go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From the most specific exception to the most general, with Exception last.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: How do you show an error to a Lightning component user?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Throw an AuraHandledException with a clear message from the @AuraEnabled method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: How do you stop a save from a trigger with a clean message?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call addError on the record. It blocks only that record and shows your message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do you rethrow an exception?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use throw e inside the catch block, or wrap it in a custom exception with the original as its cause.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: How do you get the line number of an error?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call getLineNumber on the exception.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: What does DmlException.getDmlMessage(0) return?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The error message for the first failed record in the DML operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on Apex Exception Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: Why should you avoid empty catch blocks?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They hide real bugs, so problems appear later as wrong data with no error trail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Does a caught exception roll back DML?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. If you catch it, earlier DML stays unless you roll back with a savepoint. An unhandled exception rolls back everything.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: How do you log errors that survive a rollback?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Publish a platform event set to publish immediately, or log from asynchronous code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: How do you pass the original error into a custom exception?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the constructor that takes an exception, such as new CourseFullException(e), or call initCause.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: Should controllers expose raw exception messages?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Show a friendly message to users and log the technical details for support.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: How do you test that code throws an exception?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call the method in a try block, assert that the expected exception type was caught, and fail the test if no exception was thrown.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: When would you choose addError over throwing an exception in a trigger?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For business validation. addError blocks only the bad records, while an exception fails the whole batch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_exception_definition.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Exceptions in Apex (Apex Developer Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_exception_custom.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Create Custom Exceptions (Apex Developer Guide)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Exception Handling uses try, catch and finally to react to errors.<\/li>\n\n<li>Catch specific exceptions first and Exception last.<\/li>\n\n<li>Custom exceptions extend Exception and end with &#8220;Exception&#8221;.<\/li>\n\n<li>Use addError in triggers and AuraHandledException for components.<\/li>\n\n<li>LimitException cannot be caught, so prevent it with bulk code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You have finished Level 4: Triggers and Limits. Coming soon: Level 5, starting with Apex test class basics and the 75% coverage rule.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Exception Handling is how your code reacts when something goes wrong, such as a missing record, a failed save or a bad value.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1548,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[68,98,110,91,7,97],"swapz_series":[],"class_list":["post-1551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-advanced","tag-apex","tag-exception-handling","tag-interview-questions","tag-salesforce","tag-salesforce-developer"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Exception Handling and Custom Exceptions<\/title>\n<meta name=\"description\" content=\"Apex Exception Handling explained simply: try, catch, finally, built-in and custom exceptions, addError, AuraHandledException and 21 interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Exception Handling and Custom Exceptions\" \/>\n<meta property=\"og:description\" content=\"Apex Exception Handling explained simply: try, catch, finally, built-in and custom exceptions, addError, AuraHandledException and 21 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-07T09:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"Apex Exception Handling and Custom Exceptions Explained\",\"datePublished\":\"2026-09-07T09:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/\"},\"wordCount\":1741,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-exception-handling-custom-exceptions-featured.png\",\"keywords\":[\"Advanced\",\"Apex\",\"Exception Handling\",\"Interview Questions\",\"Salesforce\",\"Salesforce Developer\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/\",\"name\":\"Apex Exception Handling and Custom Exceptions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-exception-handling-custom-exceptions-featured.png\",\"datePublished\":\"2026-09-07T09:30:00+00:00\",\"description\":\"Apex Exception Handling explained simply: try, catch, finally, built-in and custom exceptions, addError, AuraHandledException and 21 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-exception-handling-custom-exceptions-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-exception-handling-custom-exceptions-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Exception Handling featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-exception-handling-custom-exceptions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Exception Handling and Custom Exceptions Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Exception Handling and Custom Exceptions","description":"Apex Exception Handling explained simply: try, catch, finally, built-in and custom exceptions, addError, AuraHandledException and 21 interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/","og_locale":"en_US","og_type":"article","og_title":"Apex Exception Handling and Custom Exceptions","og_description":"Apex Exception Handling explained simply: try, catch, finally, built-in and custom exceptions, addError, AuraHandledException and 21 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-07T09:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"Apex Exception Handling and Custom Exceptions Explained","datePublished":"2026-09-07T09:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/"},"wordCount":1741,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-featured.png","keywords":["Advanced","Apex","Exception Handling","Interview Questions","Salesforce","Salesforce Developer"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/","name":"Apex Exception Handling and Custom Exceptions","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-featured.png","datePublished":"2026-09-07T09:30:00+00:00","description":"Apex Exception Handling explained simply: try, catch, finally, built-in and custom exceptions, addError, AuraHandledException and 21 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-exception-handling-custom-exceptions-featured.png","width":2400,"height":1260,"caption":"Apex Exception Handling featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-exception-handling-custom-exceptions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Exception Handling and Custom Exceptions Explained"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1551"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1551\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1548"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1551"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}