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
Steven HoughtalenSteven Houghtalen 

Need help with exception handling - catch statement

I get a compile error with the following code and can't figure out how to resolve.  Could use some help...thank you.

public class testexcept{
    string a = 'temp';
    string b = 'test';
    
    public static void jamen(){
        // main code here    
    }   
        
    catch (Exception e) {
    // Process exception here/    
    }
    
}
Best Answer chosen by Steven Houghtalen
Steven HoughtalenSteven Houghtalen
Thank you Nithesh.  Excellent.  I really appreciate it.

Steve

All Answers

Nithesh NNithesh N
Hi Steven, 

I believe the error is being thrown because of the absence of 'try' block. A try and catch block comes in pair. 
Try the following code ...
public class testexcept{
    string a = 'temp';
    string b = 'test';
    
    public static void jamen(){
       try { 
           // main code here   
        }
        catch (Exception e) {
           // Process exception here/    
        } 
    }      
    
}

Read more about exception handling here​ (https://developer.salesforce.com/page/An_Introduction_to_Exception_Handling)
Steven HoughtalenSteven Houghtalen
Thank you Nithesh.  Excellent.  I really appreciate it.

Steve
This was selected as the best answer
Nithesh NNithesh N
Hi  Steven, 
Please mark my solution as Best Solution to Mark it Solved !!

Best,
Nithesh