Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java exception, the error message didn't print out

 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day,

I would like to make the exception class simpler and with following try.

BaseException class:


Using inner class to call individual exception:


Test case:


my expecting result should be as follow, but somehow it doesn't print the error message? anyone can enlighten me what did i miss out?

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to achieve? What output are you expecting?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're calling the printStackTrace() that is inherited from Exception (which is probably the one it inherited from Throwable). That method doesn't know anything about your BaseException's errorMsg field. It get its message from the String argument passed to it's constructor. But since you didn't explicitly call super(String), the compiler inserted a call to super(), which means it has no message.

You need to call super(msg) in BaseException. And you could get rid of the errorMsg completely, since it's pointless.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in your BaseException. You never use the errorMsg field. The simple solution is to use what Exception already has:
 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff and Rob, You guys are awesome !!

Could you please advise me whether if i coding this way for exception making sense ? or they are better approach?

Thanks ahead !
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Defining your own exception classes definitely makes sense. Typically, you'd override all 4 constructors, so that the thrower can provide any combination of message, cause, both, or neither as appropriate to the situation.
 
Nakataa Kokuyo
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Jeff, appreciated it !
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're quite welcome!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cannot create four constructors matching those in Exception, call the initCause method internally.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic