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
Zane Prater 15Zane Prater 15 

How to handle 'No Match Found Error'?

Need some assistance to handle when matcher.group()  does not find match for that group so system error, 'No Match Found' is not thrown. I have tried the try/catch block but is not working.  

 
Best Answer chosen by Zane Prater 15
Zane Prater 15Zane Prater 15
I was able to resolve this by first checking if the find(), or matches() methods returns true before attempting to call the group() method.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Zane,

>> https://developer.salesforce.com/forums/?id=906F000000098arIAA

Can you try checking if the above-mentioned way in the link helps?

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Zane Prater 15Zane Prater 15
Thanks ANUTEJ. No, it did not help.
ANUTEJANUTEJ (Salesforce Developers) 
Can you mention where you are facing this issue as in what steps you were performing and where this error occurred?
Zane Prater 15Zane Prater 15
Sure.  I am using the pattern class to check the compile method for regex ('(\\d{8})') and update a field with that 8 digit string. Works fine when matcher.find() returns true.  The issue is the opposite, when it returns false, then I want the field to update to an empty string, but it never gets past the exception which is why I am using a try/catch block to suppress the error and just make the update for the negative result.
Carolina W 2Carolina W 2
I don't know if I undestood your problem...

It will be easier to solve if you post the code
Zane Prater 15Zane Prater 15
if(Trigger.isupdate) {
                
                string str1 = nextcase.subject;
                pattern pat = Pattern.compile('(\\d{8})');
                matcher matcher = pat.matcher(str1);
    		boolean matches = matcher.find();
                string matched;
                                                
                if(matches) {
                        system.debug(logginglevel.error,matches);
                        nextCase.PO_Number__c = matched;
                        	 } 
               
                else 
                    try {
    				nextCase.PO_Number__c = '';
					} 
                catch(DmlException e) {
    			System.debug('The following exception has occurred: ' + e.getMessage());
				}

 
Zane Prater 15Zane Prater 15
//copy/paste error for string variable:
string matched = matcher.group(1);

​​​​​​​
Zane Prater 15Zane Prater 15
Any idea of what the issue may be?  I have tried to catch StringException as well.  It seems it just can't be done and I will have to just to about this another way.
Zane Prater 15Zane Prater 15
I was able to resolve this by first checking if the find(), or matches() methods returns true before attempting to call the group() method.
This was selected as the best answer