File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Exception Handling" Watch "Exception Handling" New topic

Exception Handling

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have made a simple client server application wherein I am accepting a username and password from client and server checks whether it is a proper user or not.
Problem is when the client presses its submit button and when the request is sent to the server client again presses the stop button in the browser. In such a situation I get a SocketException at the server side. I am using simple html for client and servletrunner as server.
help me out if you have a suggesstion
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Since it is the client closing the socket, there is not a whole lot you can do about it except catch the exception. I generally try to enclose as much code as possible in doGet and doPost in a giant try - catch structure -
You could have a chain of catch statements to isolate the various sorts that might be thrown.
}catch( SocketException se ){
}catch( IOException ioe ){
}catch( Exception ex ){
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
"I generally try to enclose as much code as possible in doGet and doPost in a giant try - catch structure..."
I hear you
Except for the "giant" part
I try to keep my methods 20-30 lines long maximum. If a method cannot fit in a screen it's a signal that I should make it smaller as in separating it into logical functions.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I interpreted Bill's 'giant' word's meaning according to the cotext. Since all routing takes place from doGet(..) or doPost(..) methods if we try to put all the code in doGet(..) or doPost(..) inside a try...catch block , what it really means is it captures any exception whatsoever generated by the following methods like that. The code inside a goGet(..) may be just one line. But it's value is huge. So surrounding that one line with a try catch really means it is a 'GIANT CATCH'. The words serves it purpose in this context. This is how I thought when I read Bill's post.
maha anna
I agree. Here's the link:
subject: Exception Handling
jQuery in Action, 3rd edition