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
Ravi NagarRavi Nagar 

Problem with AccountHandler class

I did this code but developer console says "expecting right curly bracket, found catch". I don't find any missing curly bracket in my code. Please help!

​public class AccountHandler {
    
    public static Account insertNewAccount(String AccountName){
        try{  Account acct= new Account(Name=AccountName) ;
        insert acct;
        return acct;
           }
    }
    catch(DmlException e){
        System.debug('A DML exception has occurred: ' +e.getMessage());
        return(NULL)
    }
   
}
Best Answer chosen by Ravi Nagar
HawkedHawked
Download editors like notepad++ / Sublime text it shows you the bracket matching in your code. You have ended the method before ending the catch statement. 
2) There is no ; after null
3) If there are validation rules / required fields your insert will fail.
4) You cannot catch DML exception unless you modify your code to do the insert differently.
Check this : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_saveresult.htm
 

All Answers

HawkedHawked
Download editors like notepad++ / Sublime text it shows you the bracket matching in your code. You have ended the method before ending the catch statement. 
2) There is no ; after null
3) If there are validation rules / required fields your insert will fail.
4) You cannot catch DML exception unless you modify your code to do the insert differently.
Check this : https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_saveresult.htm
 
This was selected as the best answer
Ravi NagarRavi Nagar
Thank you Hawked! This exactly was the problem with my code. It is working now. I appreciate your help!