• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to throw exceptipn through servlet.

 
Rahul Ba
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a new servlet, which gets the app as input parameter if it null then throw a exception with custom message. I am trying to do but not successful.
Here is the my code snippet.




On the web page it display: You have the access...

but I want to display PARAMETER NULL EXCEPTION. if app is null

but it's not happening.

Could anybody help me.

Thanks.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the web page it display: You have the access...

but I want to display PARAMETER NULL EXCEPTION. if app is null


This means that reg != null
so the code didn't even run the try/catch block because it didn't pass the (if reg == null) test



Now, I would like to know what does this line of code print in the console, obviously it printed something since it is failing the (if reg == null)
test.

what I have typed so far really hasn't help with your issue, what's is the url to get to the "You have access..." page? that should tell you if the app request parameter is passing something to the servlet. One final thought is under what kind of condition that your app request parameter will be null, if you can recreate that than you should be able to see the exception
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are throwing an exception & catching it back...
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont use try catch ..just throw the exception..
 
Smitesh Shinde
Ranch Hand
Posts: 45
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are straightway catching the thrown exception.Moving the throw clause into catch block will help as it will help you to catch the other exceptions and throw the NullPointer one.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be catching exceptions in the servlets. Define an error handler in the web.xml and let it handle all exceptions -- and not by just silently eating them either.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic