static void Main()
{
try
{
throw new DivideByZeroException("Boom!");
}
catch (Exception ex)
{
var trace = new StackTrace(ex, true);
var frame = trace.GetFrame(0);
var targetFilename = frame.GetFileName();
var targetLineNumber = frame.GetFileLineNumber();
Console.WriteLine($"Exception occurred at {targetFilename}, line {targetLineNumber}");
if (File.Exists(targetFilename))
{
var source = File.ReadAllLines(targetFilename);
var startIndex = Math.Max(0, targetLineNumber - 3);
var endIndex = Math.Min(source.Length - 1, targetLineNumber + 1);
for (var i = startIndex; i > {lineNumber}" : $" {lineNumber}")}{source[i]}");
}
}
}
}Show HN: Decorate Java stack traces with source code snippets
11–20 of 22 posts
Re: Show HN: Decorate Java stack traces with source code snippets
#12Re: Show HN: Decorate Java stack traces with source code snippets
#13One big "off-label" use of stack traces for me is to determine the caller method of the current method. I use it in a lightly customised log function to also log the name of the method that has emitted the log.
Logback even supports including the call chain in the output, so you could include the parent method, grand parent method etc. This probably uses reflection, so it could have a negative impact for applications that need a high performance.
Re: Show HN: Decorate Java stack traces with source code snippets
#14Re: Show HN: Decorate Java stack traces with source code snippets
#15One big "off-label" use of stack traces for me is to determine the caller method of the current method. I use it in a lightly customised log function to also log the name of the method that has emitted the log.
https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/l...
Re: Show HN: Decorate Java stack traces with source code snippets
#16Re: Show HN: Decorate Java stack traces with source code snippets
#17I did this a while back for Python: https://GitHub.com/qix-/better-exceptions
Re: Show HN: Decorate Java stack traces with source code snippets
#18One big "off-label" use of stack traces for me is to determine the caller method of the current method. I use it in a lightly customised log function to also log the name of the method that has emitted the log.
If you're using Java 9+ there is an official API to do that. It's the StackWaker class. https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/l...
Re: Show HN: Decorate Java stack traces with source code snippets
#19One big "off-label" use of stack traces for me is to determine the caller method of the current method. I use it in a lightly customised log function to also log the name of the method that has emitted the log.
If I understand your use case, most loggers (like Logback) have the possiblity to automatically include the name of the method that called the log-statement. Logback even supports including the call chain in the output, so you could include the parent method, grand parent method etc. This probably uses reflection, so it could have a negative impact for applications that need a high performance. https://logback.qos.ch…
Re: Show HN: Decorate Java stack traces with source code snippets
#20Reminds me of power assertions in Groovy - one of my favorite features!
Yep they are amazing. Never been a fan of alternative JVM languages, but wrote most tests in Groovy just for this feature.
https://github.com/bnorm/kotlin-power-assert/
You just add the Gradle plugin and you're done.