public static  List<Account> GetAccountOwnerEmail(String AccountId) {
    List<Account> lstAccount = new List<Account>();
    String [] conFields = new string[]{'Email'};
    if ( !se.isFieldAccessible('Contact',conFields,FLS_Security.FLSType.ISACCESS) ) {
        throw new MyCustomeException ('You do not have sufficient privilege to perform this operation for Contact, Please contact to system admin');
    try {
        String queryString ='Select Id ,(Select Id,email from contacts) from Account where Id= :AccountId'; 
        lstAccount = database.Query(queryString);
        return lstAccount;  
    } catch ( Exception e ) {
        throw e;
  • 2
    Your question appears to be asking for other people to write code for you. SFSE is a Q&A forum where we look to help each other by asking and answering questions about real problems or errors with our code. Posting requests to write code for you will get closed. If you post what you've written so far and where you're stuck, I'm confident you'll find plenty of people willing to assist you – Himanshu May 6 '16 at 12:56
  • As told by @ninja.hk try by passing some junk value to parameter accountId like 123 it will give you error "ERROR at Row:1:Column:30 invalid ID field: 123" and execute catch block. – San May 6 '16 at 20:09

The try block contains only a string assignment statement and a query statement. The string created is straightforward, so no chance of getting an exception there.

Another statement is Database.query() which may throw an exception if the query is wrong but as the query string is hardcoded there, you cannot get a QueryException as well.

So to force an exception you will have to change the code a little bit. Instead of directly using the variable AccountId in query, first create a local variable of type Id in try block, assign the value of AccountId to it and use this variable in query.

Here if the value of AccountId is not actually an Id, a StringException will be thrown saying Invalid Id while assigning it's value to variable with Id type.

Not the answer you're looking for? Browse other questions tagged or ask your own question.