function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Parikhit SarkarParikhit Sarkar 

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.
Best Answer chosen by Parikhit Sarkar
Parikhit SarkarParikhit Sarkar

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: 

public class CustException extends Exception {
    
    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;
    }
}


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

David Zhu 🔥David Zhu 🔥
Apex executes catch blocks in sequence. Catch block identifies which type of exception can be handled in the block.
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.
Parikhit SarkarParikhit Sarkar

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: 

public class CustException extends Exception {
    
    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;
    }
}


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. 

This was selected as the best answer