You need to sign in to do that
Don't have an account?

Returning null in catch block - apex
Is returning null in catch block is best practice..
String strOpportunityRecord;
Opportunity sourceOpportunityRecord = new Opportunity();
try {
strOpportunityRecord = 'SELECT ' + Queriablefields +'Name From Opportunity where id =: sourceOpportunityId';
if (strOpportunityRecord != null) {
sourceOpportunityRecord = Database.query(strOpportunityRecord) ;
}
return sourceOpportunityRecord;
}
catch(QueryException ex) {
system.debug(ex);
return null;
}
}
String strOpportunityRecord;
Opportunity sourceOpportunityRecord = new Opportunity();
try {
strOpportunityRecord = 'SELECT ' + Queriablefields +'Name From Opportunity where id =: sourceOpportunityId';
if (strOpportunityRecord != null) {
sourceOpportunityRecord = Database.query(strOpportunityRecord) ;
}
return sourceOpportunityRecord;
}
catch(QueryException ex) {
system.debug(ex);
return null;
}
}
Best is a relative word.
If you want to swallow / disregard the exception then you would return null. i.e. you cannot do anything about the exception but still want to continue processing then this might be "best".
"best" often means doing something with an exception. Be it using .addError( ) to notify the user, throwing a custom exception, or performing some other error handling corrective logic.
So I would say this is not best practice but maybe the author was expecting to fail and didn't want to stop execution. Personally I like to know when my applications fail and swallowing the exception prevents error reporting so I would venture to say no.. not best practice but might be intentional.
Thanks for your help. If u dont mind I have another query .. Could you please help me resoving the issue.
if I am having a class
---------------------------------------------------------
public class TVRemoteControl {
// Volume to be modified
Integer volume;
// Constant for maximum volume value
static final Integer MAX_VOLUME = 50;
// Constructor
public TVRemoteControl(Integer v) {
// Set initial value for volume
volume = v;
}
public Integer increaseVolume(Integer amount) {
volume += amount;
if (volume > MAX_VOLUME) {
volume = MAX_VOLUME;
}
return volume;
}
public Integer decreaseVolume(Integer amount) {
volume -= amount;
if (volume < 0) {
volume = 0;
}
return volume;
}
public static String getMenuOptions() {
return 'AUDIO SETTINGS - VIDEO SETTINGS';
}
}
--------------------------------------------------------------
When I will write the test case -
@isTest
class TVRemoteControlTest {
@isTest static void testVolumeIncrease() {
TVRemoteControl rc = new TVRemoteControl(10);
Integer newVolume = rc.increaseVolume(15);
System.assertEquals(25, newVolume);
}
@isTest static void testVolumeDecrease() {
TVRemoteControl rc = new TVRemoteControl(20);
Integer newVolume = rc.decreaseVolume(15);
System.assertEquals(5, newVolume);
}
@isTest static void testVolumeIncreaseOverMax() {
TVRemoteControl rc = new TVRemoteControl(10);
Integer newVolume = rc.increaseVolume(100);
System.assertEquals(50, newVolume);
}
@isTest static void testVolumeDecreaseUnderMin() {
TVRemoteControl rc = new TVRemoteControl(10);
Integer newVolume = rc.decreaseVolume(100);
System.assertEquals(0, newVolume);
}
@isTest static void testGetMenuOptions() {
// Static method call. No need to create a class instance.
String menu = TVRemoteControl.getMenuOptions();
System.assertNotEquals(null, menu);
System.assertNotEquals('', menu);
}
}
--------------------------------------------
Can TVRemoteControl rc = new TVRemoteControl be initialised once and can be reused.
Regards
Your code would look cleaner if it were converted into a method and looked like this:
<pre>
Opportunity getOppo(ID sourceOpportunityId, String queriableFields) {
// queriableFields guaranteed to be in form 'fldA,fldB,...,'
Opportunity res;
try {
res = Database.query('SELECT ' + queriableFields +'Name From Opportunity where id =: sourceOpportunityId');
}
catch (QueryException e) {}
return res;
}
</pre>
<pre>
@isTest
class TVRemoteControlTest {
@isTest static void testRemoteControlActions() {
TVRemoteControl rc = new TVRemoteControl(10);
// 0.1 test increaseVolume
Integer newVolume = rc.increaseVolume(15);
System.assertEquals(25, newVolume); // 10 + 15 = 25
// 0.2 test decreaseVolume
newVolume = rc.decreaseVolume(15);
System.assertEquals(10, newVolume); // 25 - 15 = 10
// and so on
}
}
</pre>