+ Start a Discussion
RajeevlsHydRajeevlsHyd 

Display an exception message not working

I want to throw an exception when the list size is more than 5. This code is not working

--------------------------------------------------------

 

public PageReference addDealSplitAction(){

DealSplitWrapper dealSplit = new DealSplitWrapper(product);
try{
if(dealSplits.size()<6){
dealSplits.add(dealSplit);
throw new Exception('Test Common Exception'); // To throw if list size is greater than 5 ..not working
}
}catch(Exception ex) {
System.debug(ex.getMessage());
}
return null;
}

 

---------------------------------------------------------

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi Rajeev,

 

you have to use <apex:pagemessages> tag in your VF page to display exception.

 

Apex Code:

public PageReference addDealSplitAction(){
	DealSplitWrapper dealSplit = new DealSplitWrapper(product);
	try{
		if(dealSplits.size()<6){
			dealSplits.add(dealSplit);
			//throw new Exception('Test Common Exception'); // To throw if list size is greater than 5 ..not working
		}else{
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Test Common Exception');
			ApexPages.addMessage(myMsg);
		}
	}catch(Exception ex) {
		System.debug(ex.getMessage());
	}
	return null;
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90

Hi Rajeev,

 

you have to use <apex:pagemessages> tag in your VF page to display exception.

 

Apex Code:

public PageReference addDealSplitAction(){
	DealSplitWrapper dealSplit = new DealSplitWrapper(product);
	try{
		if(dealSplits.size()<6){
			dealSplits.add(dealSplit);
			//throw new Exception('Test Common Exception'); // To throw if list size is greater than 5 ..not working
		}else{
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Test Common Exception');
			ApexPages.addMessage(myMsg);
		}
	}catch(Exception ex) {
		System.debug(ex.getMessage());
	}
	return null;
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
Deepa.B.AnkaliDeepa.B.Ankali
@RajeevIsHyd,

Update it to :

if(dealSplits.size()<6){
dealSplits.add(dealSplit);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Test Common Exception')); // To throw if list size is greater than 5 ..not working
}
RajeevlsHydRajeevlsHyd

Tahnks Deepa