+ Start a Discussion
bharath kumar 52bharath kumar 52 

Display error message from Apex controller's constructor in lightning

Hi All,

I am working on converting a vf page to lightning component where i need to display error messages from a constructor.
//Following is the code in the constructor
 catch(exception ex){
        ApexPages.addmessage(new ApexPages.Message(ApexPages.Severity.ERROR,String.ValueOf(ex)));// This is for vf
        
    }

The above code works in VF. But, how do i display the message in a lightning component?

Thanks and Regards,
Bharath Kumar M
Hemant_SoniHemant_Soni
Hi Bharath,
I think you should try like below code snippt.
//Following is the code in the constructor
 catch(exception ex){
        ApexPages.addmessage(new ApexPages.Message(ApexPages.Severity.ERROR,String.ValueOf(ex)));// This is for vf

throw new AuraHandledException(ex);

        
    }
If this solution helps you please mark it solved.
Thanks
Hemant
bharath kumar 52bharath kumar 52
Thanks Hemant. Will check that