function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Unable to catch exception



I have written a controller, and a page that uses it. This method catches an exception, I am trying to pass the error to the apex : messages visualforce page .


Clicking Running this method, I get an "An unexpected error has occurred.  " page. what's wrong with my exception catching?



public void deleteOldInvoices() {

//this method is in the controller. it is run from the page try { //some code here. } } catch (Exception e) { Apexpages.addMessages(e); } }








It's causing an error because the addMessages method is expecting a ApexPages.Message as the parameter instead of an Exception.  The code below should work.  



try { //some code here. } catch (Exception e) { ApexPages.addMessage(new ApexPages.Message( ApexPages.severity.ERROR, e.getMessage()) ); }





Thanks Jessie.


I've just solved it, apparently the exception was coming from the constructor....


Actually, what you say is not quite precise.


ApexPages.Message only accepts a message, but ApexPages.Messages actually accepts an Exception object. it works fine for me...




Message Edited by beener on 02-16-2010 07:27 PM
I didn't realize that, that's useful.  Thanks Ben!