How to show user error page with customised error message

I have velocity code for creating a page and I would like to show user a page with customised error message or exception if the exception is caught.

  ##Retrieve Process file
  #try("myexception")
  #set($processDoc = $xwiki.getDocument($request.Process))
  #set($processID = $processDoc.get('ID'))
  #end
  #if ($myexception != '' || $proecssID == '')
    #displayException("ID Not Found")
    {{warning}}ID cannot be found for process{{/warning}}
  #end

Does anyone know how to do this?

Hi, yes it’s possible, we have velocity try directive for that, see https://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity%20Module#HVelocityDirectives

Thanks! Somehow #displayException never work for me. It keeps giving me nullpointer exception. However, the try and catch works just fine.

There are 2 velocity macros defined in macros.vm and that you can use:

###
### Displays an exception.
###
### @param title the message to display to the user
### @param exception the exception to display when the user clicks on the message
#macro(displayException $title $exception)
<div class="xwikirenderingerror" title="Click to get more details about the error" style="cursor: pointer;">
  $title
</div>
<div class="xwikirenderingerrordescription hidden">
  <pre>$escapetool.xml($exceptiontool.getStackTrace($exception))</pre>
</div>
#end

###
### Generically display exceptions that have not been handled at the right level and have now bubbled up to the UI.
###
### This is a convenience macro, see #displayException.
###
### @param exception The exception to format for displaying
#macro(displayUnhandledException $exception)
  #set ($sentence1 = 'Unexpected error.')
  #set ($sentence2 = 'Contact your administrator or <a href="https://jira.xwiki.org">report the issue</a>.')
  #set ($sentence3 = 'Click this box to get technical details.')
  #displayException("${sentence1} ${sentence2} ${sentence3}", $exception)
#end

So you need a title and an exception in the call to the #displayException() macro (but not the other one). Fixing the example.