+ Start a Discussion
maxoutmaxout 

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?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sonali BhardwajSonali Bhardwaj

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

_Prasu__Prasu_

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];

Shashikant SharmaShashikant Sharma

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.

Sonali BhardwajSonali Bhardwaj

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());
}

 

This was selected as the best answer