File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes e.printStackTrace(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "e.printStackTrace();" Watch "e.printStackTrace();" New topic
Author

e.printStackTrace();

Tahir Ahm
Greenhorn

Joined: May 17, 2012
Posts: 6

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

Joined: Oct 01, 2001
Posts: 2861
    
  11

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

Joined: May 17, 2012
Posts: 6


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

Joined: Oct 01, 2001
Posts: 2861
    
  11

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

Joined: Mar 30, 2012
Posts: 124

A better option can be checking whether the message in Exception Contains a particular message.
Tahir Ahm
Greenhorn

Joined: May 17, 2012
Posts: 6


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

Thank you guys, looking forward to learn more things from this forum.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: e.printStackTrace();