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
e r i c.ax249e r i c.ax249 

How to get the line number of exception when using try/catch

I've written an Apex Service that will be called from an inhouse application.  I have a try/catch block that catches any exceptions and returns an error type that I've defined.  I also send an email notification out.  I know you can grab the exception type and message with .getTypeName() and .getMessage(), but how do I get the exception line number?  That would be handy information to send in the error notification email.  If I don't handle the error, the service returns the exception type, exception message, class name, class method, and line number in the faultstring element.  I don't see anything in the Apex documentation that would allow you to access the line number when catching the exception though.  What gives?  Is this available?



It's not possible unfortunately(not that I know of at least). I resort to oldschool database debug methods eg.


public class MyClass{

private String lineMsg;




// Some logic

lineMsg = 'About to enter if-statement. myVal: '+myVal);



// more logic


  lineMsg = 'Fetching records for ....';


}catch(system.exception e){

  System.debug(e.getMessage+ ' - lineMsg: '+lineMsg);


It's crude, but you have to do what you can with what you have.






e r i c.ax249e r i c.ax249

@saadis01 - that was not available at the time I created this thread, but I'm glad they added it.


@eric, I posted it so that next time someone else coming looking for an answer will actually see how's it done, unlike me who had to continue the search :)



Yeah, I always find it better to reply to a five year old post when the answer isn't there, and it's a frustrating issue.

Now how about a getClassName() so we know which class that line number is in?
Soumyaranjan Pati 10Soumyaranjan Pati 10
use like this:
}catch(Exception e){
            System.debug('Exception e '+e);
            System.debug('Exception e '+e.getLineNumber());