You need to sign in to do that
Don't have an account?
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.
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
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() );
}
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?
Suggestion : If you suggest some solution, must be updated as per releases and new issues that might can come.!!
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). Troy ~ Seattle