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

How to write testmethod for try catch exception

Hi , I am writing a testmethod.But i am not able to cover the lines under try catch statement.Please advice me.


public class CreditCheck {
 public String CustCheck {get; set;}
 public Credit_Check__c CCheck {get; set;}
 public RecordType recType = new RecordType();
 public boolean limited {get; set;}
 public boolean Nonlimited {get; set;}
 public boolean PreviousAddressReq {get; set;}
 public Opportunity oppr;
 public CreditCheck(ApexPages.StandardController stdController) {
    this.CCheck = (Credit_Check__c)stdController.getRecord();
 public PageReference save(){
    	try { 
	    // if(CCheck.RecordtypeId == '012200000009PMEAA2'){
	     	list<Opportunity> creOppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c limit 1];
	     if(creOppr.size() > 0 ){
	     	Opportunity oppr =null;
	     	Oppr = creOppr[0];
	     	//	String opprName = 'Opportunity:' + Oppr.Name + '  Business Type:' + Oppr.Account.Business_Type__c + ' Supplier:' + Oppr.New_Supplier__c + '  Product:' + Oppr.Product_Name__c + '  Addr Billing:' + CCheck.Billing_Address__c;
	     	//CCheck.Billing_Address__c.addError(' ' + opprName );
	    	//return null;
		     if(Oppr.Account.Business_Type__c == 'Limited Company' || Oppr.Account.Business_Type__c == 'Plc' || Oppr.Account.Business_Type__c == 'Charity'){
		  			 if(Oppr.New_Supplier__c == 'Eon' && Oppr.Product_Name__c != 'Bespoke Price' ){ 
		  			 	if(CCheck.Limited_Company_Account__c == '' ){
            CCheck.Limited_Company_Account__c.addError('Please Enter the Account Name');
             return null;
            if(CCheck.Registered_Address__c == ''){
            CCheck.Registered_Address__c.addError('Please Enter the Full Registered Address');
             return null;
            if(CCheck.Date_of_Incorporation__c == null){
            CCheck.Date_of_Incorporation__c.addError('Please Enter the Date of Incorporation');
             return null;
             if(CCheck.Company_Registered_Number__c == ''){
            CCheck.Company_Registered_Number__c.addError('Please Enter the Full Company Registered Number');
             return null;
            if(CCheck.Phone_Number__c == ''){
            CCheck.Phone_Number__c.addError('Please Enter the Full Phone Number');
             return null;
		    if(CCheck.Billing_Address__c == ''){
		    CCheck.Billing_Address__c.addError('Please Enter the Full Billing Address');
		     return null;
Static testmethod void testsave(){
    	Credit_Check__c cre =new Credit_Check__c();
    	 ApexPages.StandardController con = new ApexPages.StandardController(cre);
    	ApexPages.Pagereference pageRef = Page.CreditCheckEdit;
    	CreditCheck cat = new CreditCheck(con);;
    	Opportunity o = new Opportunity();
    	o.New_Supplier__c ='Eon';
    	cre.Limited_Company_Account__c ='';
    	Account ac = new Account();
    	ac.Business_Type__c ='Limited Company';




Has your code been truncated?  I can't see the catch block in the sample you posted.


Whether you can test this depends on whether its something that can occur based on user input etc (bad data) or if you are trying to catch unexpected errors.


If the former, you can should be able to set bad test data up and get the exception to be thrown.

If the latter, you can use the Test.isRunningTest method to determine if you are running inside a unit test and throw a random exception.