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

What will be the output in the debug log in the event of a QueryException during a call to the aQuery method in the following example?
class myClass {
class CustomException extends QueryException {}
public static Account aQuery() {
Account theAccount;
try {
system.debug(‘Querying Accounts.’);
theAccount = [SELECT Id FROM Account WHERE CreatedDate > TODAY];
} catch(CustomException eX) {
system.debug(‘Custom Exception.’);
} catch(QueryException eX) {
system.debug(‘Query Exception.’);
} Finally {
system.debug(‘Done.’);
}
Return theAccount;
}
}
A. Querying Accounts. Query Exception.
B. Querying Accounts. Custom Exception.
C. Querying Accounts. Custom Exception. Done.
D. Querying Accounts. Query Exception. Done.
I thought the answer would be D but the correct answer is C. Can anyone please explain why? Thanks in adv.
class CustomException extends QueryException {}
public static Account aQuery() {
Account theAccount;
try {
system.debug(‘Querying Accounts.’);
theAccount = [SELECT Id FROM Account WHERE CreatedDate > TODAY];
} catch(CustomException eX) {
system.debug(‘Custom Exception.’);
} catch(QueryException eX) {
system.debug(‘Query Exception.’);
} Finally {
system.debug(‘Done.’);
}
Return theAccount;
}
}
A. Querying Accounts. Query Exception.
B. Querying Accounts. Custom Exception.
C. Querying Accounts. Custom Exception. Done.
D. Querying Accounts. Query Exception. Done.
I thought the answer would be D but the correct answer is C. Can anyone please explain why? Thanks in adv.
Hi David,
Thanks for the swift reply. I have figured out after testing the code in my Org that there is a fault in the question. You are right about the fact that "All types of exceptions are inherited from Exception. e.g. DMLException". However, QueryException class resides inside System namespace and it cannot be extended because it is of Non-Virtual or Non-Abstract type. Thus, the code construct should be as follows:
This will return the following results in the debug log -
Querying Accounts.
Query Exception.
done
Hence, my assumption that the correct answer is D was right.
Regards,
Parikhit.
All Answers
in your case, the code tries to check if it is a customexception type then queryexception type. When it runs, it finds this is a queryexception type, since customexception is an inheriten type of queryexception, it s a more detailed queryexcption, so the first black catches the exception.
All types of exceptions are inherited from Exception. I.e. DMLExceprion. When writing the code, we usually catch DMLexception then Exception. Otherwise, we won't catch DMLexception forever.
I think C is correct.
Hi David,
Thanks for the swift reply. I have figured out after testing the code in my Org that there is a fault in the question. You are right about the fact that "All types of exceptions are inherited from Exception. e.g. DMLException". However, QueryException class resides inside System namespace and it cannot be extended because it is of Non-Virtual or Non-Abstract type. Thus, the code construct should be as follows:
This will return the following results in the debug log -
Querying Accounts.
Query Exception.
done
Hence, my assumption that the correct answer is D was right.
Regards,
Parikhit.