You need to sign in to do that
Don't have an account?
Covering the Catch block in my Class
In my class I have a PageReference that does a SOSL search and I have a try catch block around it (purely for the off sinaro that search itself might fail...) but my code around it checks for null and if it is less than 3 in lenght.... so i cant seem to find a way to cover that block. Do i even need the try{} catch{} at this point?
Here is my method:
/*SOSL search for cases/contacts with the phone number in the param*/ public PageReference getCaseData(){ string p = getAniParam(); if(randishelperclass.checkIfNullOrEmpty(p)){ if(p.length() > 3){ string ph = p + '*'; try{ List<List<sObject>> searchlist = [find :ph IN PHONE FIELDS RETURNING Case (id,casenumber,SuppliedPhone,subject,product__c,contact.name,contact.id,status,LastModifiedDate ORDER by LastModifiedDate desc limit 25),Contact(id, phone, email, name ORDER by name desc limit 25) limit 50]; cseResults = ((List<Case>)searchlist[0]); conResults = ((List<Contact>)searchlist[1]); if(cseResults.size() > 0 && conResults.size() > 0){ renderCseResults = true; renderConResults = true; bRendered = false; }else if(cseResults.size() > 0 && conResults.size() == 0){ renderConResults = false; renderCseResults = true; bRendered = true; }else if(cseResults.size() == 0 && conResults.size() > 0){ renderConResults = true; renderCseResults = false; bRendered = false; } return null; }catch(exception e){ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Fatal , 'Oops... ' + e); ApexPages.addMessage(msg); return null; } }else{ PageReference ref = new PageReference('/apex/ticketquickcreate?CAS=1'); ref.setRedirect(true); return ref; } }else{ PageReference ref = new PageReference('/apex/ticketquickcreate?CAS=1'); ref.setRedirect(true); return ref; } }
I'd say no, but defensive programming is a good idea. Perhaps you can just leave that catch block uncovered by your unit tests.
Maybe look into a way to modify the type of PHONE FIELDS, causing data type mis-match?
Thanks, I'll look into that...
I found some documentation on throwing an exception, or making your own exception class since
throw new Exception;
is a Fail and wont save.
But I think I do need the catch statement only for one reason: if the salesforce search index dies (which happened last week) and thus the SOSL wouldn't work...