0

I wonder, is it possible to show exceptions or any errors, like "Null pointer exception" in visualforce? For now all errors refresh the page and displaying error on the blank page. I want for my users, if any system error appears, they would not lost input data in the visualforce page. Iknow about but it only for validating. It is not for my case. My page submits for approval many records at once for 20 approvals with different criterias.

Any Ideas?

2 Answers 2

1

Presuming you can't somehow avoid an exception (e.g. NullPointerException is almost always avoidable), you would want to wrap your code in code like this:

public ... someVFAction() {
  Savepoint sp = Database.setSavepoint();
  try {
    // your code here //
  } catch(Exception e) {
    ApexPages.addMessages(e);
    Database.rollback(sp);
  }
}

Make sure you include an apex:pageMessages on your page to show messages.

0
0

You can use apex <apex:pageMessages /> tag,

eg:

<apex:page>
   <!-- some code -->
   <apex:form >
      <apex:pageMessages />
   <!--- some code -->
   </apex:form >
</apex:page>

On Controller side you need to pass exception/error to apex:pageMessages like this,

//.. some code.. 
try{

} catch (Exception e){

    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error:' + e.getMessage());
}

Any unhanded exception or system error will be also displayed by default in page message. Values supported for Severity are: 'confirm', 'info', 'warning', 'error'

Reference

You must log in to answer this question.

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