Throw Custom Error Message from Apex to Lightning

Throwing Custom Error Message from Apex to your Lightning or LWC component is quite easy and standard. While saving a record, you never know what exception may occur. It may be a Validation rule exception or Required Field Exception or any other Apex Exception. With Aura Handled Exception, you can throw these exceptions right on the screen and can show them in Toast Messages.

To throw the multiple Custom Exceptions from Apex, first, create a Custom Exception class.

Now, you can refer to the below code to throw custom exceptions. Since we are using try-catch block here, any standard exception if occurred will also be thrown to UI.

Now on Lightning, you can refer to the below code to show errors.

So, in this way you can show Custom Error Message on Lightning UI. With this method, you can throw your custom message as well as standard will be thrown automatically.

If you want to throw only a single custom message, you can directly throw it from auraHandledException only.

To refer to the Salesforce documentation, Click Here

 

Also Check:

For any queries or suggestions, comment below.

Cheers … Happy Coding … 🙂

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments