11

I have a exception class where in which i want to pass the current line number

 line 70:
 line 71: throw new 
 LightException(FailureType.NOT_FOUND,this.getClass().getName(),linenum);

Is there a way to get linenum as 72 here without hardcoding it? Does eclipse offer anything that would get replaced into hardcoded line numbers at compile time.So that i don't have to put ugly hard-code line numbers

class LightException(FailureType type,String className,int lineNum) extends RuntimeException 
{
LightException(FailureType type,String className,int lineNum){..
//
}
@Override
@Override public Throwable fillInStackTrace() { 
return this;
 }
}

I do not need to log the entire stack trace and unnecessarily filling the stack trace for all exceptions.I want to add the line number from where the exception was thrown. Any code which can be resolved at compile time into constants?

If not then I can write a simple utitly to pre-process my code which can read lines and replace a special constant _my_line_num by the line number but something should exist.

I feel some build tool like gradle can do this.

asked Jun 23, 2015 at 19:06
4
  • AFAIK I don't think Eclipse offers anything like this. Commented Jun 23, 2015 at 19:09
  • 1
    Refer this Commented Jun 23, 2015 at 19:12
  • 1
    I didn't want that.If i have to fill the stack trace and get the line number of it.There is no point in avoiding it.I would rather have a simple utility do it for me Commented Jun 23, 2015 at 19:14
  • Why don't you want to use the stack trace? Is it just a performance question? If so make sure it's a real issue before you make something complicated Commented Jun 23, 2015 at 19:52

1 Answer 1

0

I am not sure if it is good solution or not but you can place this:

int linenumber = Thread.currentThread().getStackTrace()[2].getLineNumber();

as a parameter in your exception's contractor and display it as you need.

answered Jun 23, 2015 at 19:19
Sign up to request clarification or add additional context in comments.

3 Comments

Haven't you read the OP's request I do not need to log the entire stack trace and unnecessarily filling the stack trace for all exceptions.
@Madhan you can give it a try
This is very slow.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.