0

Class: (Custom Exception)

public class CustomException extends Exception {

}

Using as below in my help class:

throw new CustomErrorException('No other characters allowed');

This is working perfectly both from API and UI. But when an exception occurs, it is showing the error message including class name, error line (as shown in below screenshot). However, I would like to display only the error message and omit the other part. As this is custom exception, I cannot use ApexPages.Severity.FATAL.

Are there any ways to achieve this. Thanks

Error Message

1 Answer 1

4

You still need to use a catch block in tandem with ApexPages.addMessages if you want that sort of clean interface. The fact that it's a custom exception by no means indicates you can't use this approach.

Demo

public with sharing class MyController
{
    public class CustomException extends Exception { }
    public MyController()
    {
        try
        {
            throw new CustomException('All your base are belong to us');
        }
        catch (CustomException error)
        {
            ApexPages.addMessages(error);
        }
    }
}
--------------------------------------------------------------
<apex:page controller="MyController">
    <apex:pageMessages />
</apex:page>

You can replace the throw statement above with any operation that could result in your custom Exception type, including DML Operations inducing that behavior.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .