You need to sign in to do that
Don't have an account?
maxout
test method stopped by a soql which is in a try catch block
public id dummy;
public pagereference somemethod()
{
try{
sometype obj=[select id from sometype where id=: dummy];
}catch(e)
{
apexpage.addMessage();
}
return null;
}
testMethod()
{
set dummy to null;
call to somemethod();
}
Hi there,
When save, the test stop at the call to somemethod() with error:
System.QueryException: List has no rows for assignment to SObject.
But this error is supposed to be catched in the method itself.
Any idea appriciated?
There might be some exception which cannot catch but this is not one of them.
I think you are not catching proper exception.
Use
try {
contact c = [select id from contact where name='111'];
}
catch(System.QueryException e) {
system.debug(e.getmessage());
}
All Answers
you should write that in following way to avoid that exception. It is thrown as queryresult is null. Make it array or limit it 1 in query.
sometype[] obj=[select id from sometype where id=: dummy];
Hi,
No, there are some exceptions that can not be catch, it is one of them.
Use a list instead and check isEmpty on it to check any record is returned.
There might be some exception which cannot catch but this is not one of them.
I think you are not catching proper exception.
Use
try {
contact c = [select id from contact where name='111'];
}
catch(System.QueryException e) {
system.debug(e.getmessage());
}