Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Java exception, the error message didn't print out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java exception, the error message didn Watch "Java exception, the error message didn New topic
Author

Java exception, the error message didn't print out

Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 184
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

Joined: Jul 27, 2010
Posts: 1157

What are you trying to achieve? What output are you expecting?


~ Mansukh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

The problem is in your BaseException. You never use the errorMsg field. The simple solution is to use what Exception already has:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 184
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 24, 2011
Posts: 184
Thanks again Jeff, appreciated it !
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're quite welcome!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
If you cannot create four constructors matching those in Exception, call the initCause method internally.
 
 
subject: Java exception, the error message didn't print out
 
Similar Threads
NX: URLy Bird 1.3.1 Explicit Fatal Exception Handling
Why do I get those exceptions? Please HELP....
Exception Handling
how to read and show a byte array?
JMS Help: send a queue message with reply