• Post Reply Bookmark Topic Watch Topic
  • New Topic

What Line of Code Created Exception  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know what specific line of code in a try catch block generated the exception. The following just displays NULL on second two lines.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the javadoc for Exception you will find that which you seek.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. you should be using a logger rather than System.out for this sort of thing.
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:If you look at the javadoc for Exception you will find that which you seek.


Read the doc. Do I assume that because I get NULL that it is because the 'cause' cannot be determined?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cause and the message are parameters that are passed to an Exception (either through the constructor or also possibly through setter methods).

The message is just a String that is meant to describe why the exception was thrown, but can say whatever the author of the code that threw the exception wants it to.

The cause of an Exception is a Throwable. It is used when catching a low level exception and the throwing a higher level exception from the catch block.

Neither of those things will directly contain line numbers. They are null because presumably they were not set. Line numbers will only be available if you compiled in debug btw.
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:The cause and the message are parameters that are passed to an Exception (either through the constructor or also possibly through setter methods).

The message is just a String that is meant to describe why the exception was thrown, but can say whatever the author of the code that threw the exception wants it to.

The cause of an Exception is a Throwable. It is used when catching a low level exception and the throwing a higher level exception from the catch block.

Neither of those things will directly contain line numbers. They are null because presumably they were not set. Line numbers will only be available if you compiled in debug btw.


Do you have any other suggestion to help me pin point the line of code that causes an exception in a try/catch block that wraps several lines of code?

Earlier using a logger was suggested. Would that do it? If so what logger? log4j?
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to print the stack trace.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!