This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

e.printStackTrace();

 
Tahir Ahm
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have a section in my java code, where i recieve unknown web service exceptions. These exceptions contains different error messages such as 'User Password expired', 'No User ID Found'... etc

I need to categorise these error messages and want to display specific error on-screen.

All though these errors contained in variable 'e', when used in logger statements, the logs are getting generated.

Is there a way, where we can use if statements on e and display specific errors? I tried,l but its not working..

Any suggestions.
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRanch!

Yes, of course you use if statements on anything that evaluates to a Boolean (i.e., true or false). What if statements were you trying that didn't work?
 
Tahir Ahm
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The exception that was logged in log files was - javax.xml.ws.soap.SOAPFaultException: javax.xml.ws.soap.SOAPFaultException: The password is expired.

So, I used the below 'if' statement..

Logger.getLogger(this.getClass().getName()).log(Level.ERROR, "Error Log" + e);
if(e.equals("javax.xml.ws.soap.SOAPFaultException: javax.xml.ws.soap.SOAPFaultException: The password is expired."))
request.setAttribute("errorMessage", res.getString("label.bean.errorcodePE"));
else
.
.
.
.



Am I Missing something here, the above statement is skipped and control is going to 'else' part...

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, there you're comparing an Exception to a String, which will always be false. e.getMessage().equals(...) should work better.
 
Anurag Verma
Ranch Hand
Posts: 167
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better option can be checking whether the message in Exception Contains a particular message.
 
Tahir Ahm
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

e.getMessage().equals(...) - it worked... thanks for valuable input....

Thank you guys, looking forward to learn more things from this forum.
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad that worked for you! I will say though that categorizing an exception in Java code by looking at its message is not a best practice. It's fragile for one thing. If a new version of whatever created the SoapFaultException changes the wording of the error message, your code will break. I would look at methods like SoapFaultException.getFaultCode() and SoapFaultException.getFaultString() and see if you can get enough information from them for your purposes. The fault code, especially, would be less likely to change without notice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic