+ Start a Discussion
UmapenUmapen 

redirect to different page in catch block

In my controller the query some time returns 0 rows. I have a try/catch block. I want to redirect to error page int he catch block. this page is going to be displayed on Sites.

How can I do that?

   ** I already set the error page assignment to my custom pages 

   ** I have multiple conditions where I want to display different error messages

 

try{ myacct =[select id, name, customid__c from Account WHERE rating__c =:rate and completed__c !=true LIMIT 1]; }catch(System.Exception e) { System.debug(e); PageReference error = page.noRateError; error.setRedirect(true); }

But this is not working.  I have this call in my custom controller constructor.

 

how can I redirect to error page in catchblock? Or other ways I can acheive this.

 
Message Edited by Umapen on 01-07-2010 09:24 AM
bmabma

For the redirect to happen, you have to return the pageReference object, "error" in this case.

 

You can create a method

public PageReference someMethod() { //a null pageReference would refresh the current page PageReference error = null; try { // the query statement } catch (System.Exception e) { //the error page where you want to redirect error = page.noRateError; error.setRedirect(true); } return error; }

 

similar to the one below:

 

 

UmapenUmapen

Thanks for the quick response. But my query in constructor method.  can I still change the way you suggested with out loosing current functionality

public class rateController{ public rateController{ try{ myacct =[select id, name, customid__c from Account WHERE rating__c =:rate and completed__c !=true LIMIT 1]; }catch(System.Exception e) { System.debug(e); error = page.noRateError; error.setRedirect(true); } } public PageReference errorpage(){ return Page.noRateError; } }

 

bmabma

You can use the action attribute on the page component to do the redirect with a slight change to the code you posted.

 

Updated Controller:

 

public class rateController { public PageReference error = null; public rateController(){ try { // your query } catch(System.Exception e) { System.debug(e); error = page.noRateError; error.setRedirect(true); } } public PageReference errorPage() { return error; } }

 


 

 

Here is the page:

 

<apex:page controller="{!rateController}" action="{!errorPage}"> </apex:page>

 

 

 

UmapenUmapen

Thanks for the promt response. But I already have a page for this controller.  I tried your code its not working.

 

  I do not know where I can give the action in the page? 

 

I think I will try to create a outputpanel with error message in the same page instead of trying to display/redirect the page.

bmabma

If you look at the page example I provide, there's an action attribute on the page component (apex:page).

 

 

"rateController" is a dummy controller. You can replace it with your conroller, just make sure the contructor has something to what I posted.

 

Also, if you just paste my code, you won't have an error, since there is no query statement. The page that display would be blank.