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
spitzergspitzerg 

Try/Catch for QueryException ignored

I am trying to run a query where I expect a single object to be returned under normal conditions. However, there is the possibility that invalid data will be submitted in which case the query will not return anything. Rather than handling this by getting a list, checking its size and then proceeding it seems sensible to simply try and return the single object and catch an exception if one arises using the following:

 

 

private static Licensable_Product_Release__c parseProductInformation( XmlStreamReader reader ) {

String identifier, version;

 

// Code to get identifier and version...

 

try {

return [SELECT Name FROM Licensable_Product_Release__c WHERE Licensable_Product__r.Identifier__c = :identifier AND Version__c = :version];

} catch( QueryException e ) {

System.debug( e.getMessage() );

}

 

return null; 

 

The problem is in my test case I intentionally feed it data so that the query will fail and at that point get:

 

 

Debug Log:

 

*** Beginning Test 1: XmlProductUpdatesRequestTransformer.static testMethod void test()

 

20091030045250.098:Class.XmlProductUpdatesRequestTransformer.parseProductInformation: line 63, column 12: SOQL query with 0 rows finished in 7 ms

System.QueryException: List has no rows for assignment to SObject

 

Unless I'm not understanding something here, it seems that my try/catch is being completely ignored. I've searched the forums and have found people getting this error without having a try/catch block but then it seems that adding a try/catch has solved the problem.

 

 

Any help greatly appreciated, thanks. 

 

Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

Hello spitzerg;

Your Exception is not caught because it occurs at the line where you return the value.

Change your try-catch block as below.

 

 

try {

Licensable_Product_Release__c myRelease = new Licensable_Product_Release__c();

myRelease = [SELECT Name FROM Licensable_Product_Release__c

WHERE Licensable_Product__r.Identifier__c = :identifier AND Version__c = :version];

return myRelease;

} catch( QueryException e ) {

System.debug( e.getMessage() );

}

 

 

 

 

All Answers

prageethprageeth

Hello spitzerg;

Your Exception is not caught because it occurs at the line where you return the value.

Change your try-catch block as below.

 

 

try {

Licensable_Product_Release__c myRelease = new Licensable_Product_Release__c();

myRelease = [SELECT Name FROM Licensable_Product_Release__c

WHERE Licensable_Product__r.Identifier__c = :identifier AND Version__c = :version];

return myRelease;

} catch( QueryException e ) {

System.debug( e.getMessage() );

}

 

 

 

 

This was selected as the best answer
spitzergspitzerg

Thanks for the quick response, that worked!

 

It does seem like you should be able to catch exceptions in a return statement, I suppose the other option is I could probably catch the exception in the calling function? 

Abhishek915Abhishek915
Not worked. Wasted time using this solution. 
Suggestion : If you suggest some solution, must be updated as per releases and new issues that might can come.!! 
Troy CenterTroy Center
Abhi that wasn't very nice. I just saw this post, I thought I would bump this since it's been six years and nobody has responded and I needed this today and... wow.
 
Spitzerg, Try block must have at least one catch block or a finally block. You need to declare the List outside of the Try block though. 
I don't know what "XmlStreamReader reader" is... 

Try this: (Seems a lot easier to me).
private static Licensable_Product_Release__c parseProductInformation( XmlStreamReader reader ) {
String identifier, version;

List<Licensable_Product_Release__c> theList = new List<Licensable_Product_Release__c>(); 
try{
     theList = [SELECT Name FROM Licensable_Product_Release__c WHERE 
     Licensable_Product__r.Identifier__c = :identifier AND Version__c = :version];
} 
finally{
     return theList;
}
Troy ~ Seattle