How to handle error in Aurahandledexception in Salesforce

by Rijwan Mohmmed

Hello friends , today we will learn how to show custom error message or proper way of get error message in catch block in Lightning web component. Many times you get error but it’s not going in catch block because we return as a string or object and we do some custom code in try block to show error.

To use Aurahandledexception we easily throw an error instance of it from within Apex. By this way we also supply custom error also And this custom error goes to lightning web component method catch block , where we show proper error toast message

Lets Understand with simple example :

AuraExceptionTechdicerLWC.html :

	<div class="slds-tabs_card">
		<div class="slds-page-header">
			<div class="slds-page-header__row">
				<div class="slds-page-header__col-title">
					<div class="slds-media">
						<div class="slds-media__figure">
							<span class="slds-icon_container slds-icon-standard-opportunity">
								 <lightning-icon icon-name="standard:recipe" alternative-text="recipe" title="recipe"></lightning-icon>
						<div class="slds-media__body">
							<div class="slds-page-header__name">
								<div class="slds-page-header__name-title">
										<span>Show Exception in LWC</span>
										<span class="slds-page-header__title slds-truncate" title="Recently Viewed">TechDicer</span>
				<div class="slds-page-header__col-actions">
					<div class="slds-page-header__controls">
						<div class="slds-page-header__control">
							<ul class="slds-button-group-list">
									<lightning-button name="ShowException" label="Show Exception" onclick={showException} variant="brand"

AuraExceptionTechdicerLWC.JS :

import {LightningElement} from 'lwc';
//Import our Apex method
import updateContact from "@salesforce/apex/AuraExceptionTechdicer.updateContact";

//Only import this if you want to show your error as a Toast message
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class AuraExceptionTechdicerLWC extends LightningElement {

           //do something
       .catch((error) => {
           //Let's send the user a toast with our custom error message
           const evt = new ShowToastEvent({
               title: "Yikes!",
               message: error.body.message,
               variant: "error",

AuraExceptionTechdicer.cls :

public class AuraExceptionTechdicer {
   public static void updateContact() {
        try {
           Contact con = [SELECT Id, LastName FROM Contact LIMIT 1];

           //Set Contact Last Name to null to trigger an exception
           con.LastName = null;
           update con;
       } catch (Exception e) {
           //Add your custom error messsage to the AuraHandledException, you can add any string here
           throw newMessageException('It seems we can\'t update your Contact right now | Error : ' + e.getMessage());

   private static AuraHandledException newMessageException(String message) {
    AuraHandledException e = new AuraHandledException(message);
    return e;

What’s your Reaction?

You may also like

Leave a Comment