Reputation: 285
i got an Exception thown and displayed on the console like this :
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:148) ~[na:na]
at com.amfinesoft.epr.stocks.Runner.inferStateFor(Runner.java:806) ~[classes/:na]
at com.amfinesoft.epr.stocks.Runner.process(Runner.java:417) ~[classes/:na]
at com.amfinesoft.epr.stocks.Runner.retrieveStocksFromEPRAndProcess(Runner.java:286) ~[classes/:na]
at com.amfinesoft.epr.stocks.Runner.run(Runner.java:183) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:775) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:765) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at com.amfinesoft.epr.stocks.Main.main(Main.java:17) ~[classes/:na]
then, i'd like to put into a variable, the last line of my trace
at com.amfinesoft.epr.stocks.Main.main(Main.java:17) ~[classes/:na]
in order to write the specific line in an audit. How could i do that ?
Upvotes: 0
Views: 115
Reputation: 111
You can catch
the exception and get the stack trace with Throwable.getStackTrace()
. This returns a StackTraceElement[]
(from java.io
) that contains each line of the stack trace
try {
// your code here
} catch (NoSuchElementException e) {
StackTraceElement[] trace = e.getStackTrace();
String lastLine = trace[trace.length - 1].toString();
}
Upvotes: 4