Is there a method similar to the Limit Method called getScriptStatements that will return the actual line number of the trigger or class its being called from?

I'm working on a custom error handler class where having this functionality would be very helpful. I'd like to be able to have a piece of code I can "drop in" that doesn't require me to hard code the line number that it's being called from.

4 Answers 4


I don't understand the comment that talks about stopping the trigger or class from running as the code would handle rather than propagate the Exception. Probably needs to look something like this though to record the right line number:

public class Handler {
   private class HandlerException extends Exception {
   public static void recordWithLineNumber(String message) {
       try {
           throw new HandlerException();
       } catch (HandlerException e) {
           String raw = e.getStackTraceString();
           // Stack trace will start with line where thrown in this class so parse
           // to identify line where Handler.recordWithLineNumber was called from
           Integer line = ...
           record(message + ' (line was ' + line + ')');
   public static void record(String message) {
       // E.g. send email (providing no rollback)
  • I want to return the line number string to add to an error string I'm building was the reason behind the comment about not wanting to throw the actual exception, but instead to catch it. Thank you, your code is something I can use to do exactly what I want in handling caught exceptions!
    – crmprogdev
    Mar 10, 2014 at 18:37
  • You don't need to throw the exception to get the stack trace. Just Exception e = new Exception(); e.fillInStackTrace(); StackTraceElement[] stack = e.getStackTrace(); should get you what you need (assuming this is Java...) Mar 10, 2014 at 20:55
  • @DarthAndroid Its Apex (triggers etc) and Salesforce haven't exposed those Java methods in Apex.
    – Keith C
    Mar 10, 2014 at 21:02

The exception class contains this information. You will need to throw an exception, catch it, and use the getLineNumber method which is a member of the Exception class.

  • That was the only method I'd seen and hadn't quite figured out how to capture it into a string I could use with my own reporting system when I've handled an exception that I want an Admin to follow up on.
    – crmprogdev
    Mar 10, 2014 at 14:30
  • You could implement your own exception class that turns the line number to a string (String.valueOf) and then inserts that information somewhere into a case where it can then be routed to the correct developer. This way your catch block is just one line of code to log the error.
    – Phil Rymek
    Mar 10, 2014 at 14:46
  • I'm trying to avoid an exception from being thrown that stops a trigger or class from running. For that reason, your suggestion would defeat my purpose. My handler sends an email rather than writing to the debug log. It primarily catches data entry/corruption problems; particularly when there's been bulk entry as opposed to entry through the UI.
    – crmprogdev
    Mar 10, 2014 at 14:59

There is no method or "magic variable" (like C's __LINE__ macro) that exposes the current line number.


Oh man, if you find this way it would be really cool. I was looking for such a thing for my own log system. Another thing is that we insert a log object into sfdc and we want all process don't stop.
What we end up with is unique message we put for particular error.
Think of this in this way. You don't surround your code with try/catch, so you know where exactly you're 'handling' error. If you have the same message which you put after each line of code, just add i++ after a message.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .