Results 1 to 4 of 4
- 11-26-2007, 07:13 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
TCPIP Client (handling specific exceptions)
I have a TCPIP client with following code.
I want to catch specific exceptions for Port and for hostname. Please advice.Java Code:try { smtpSocket = new Socket("javabook", 22122); os = new DataOutputStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (Exception ex) { System.out.println("Some error has occured"); }Last edited by ravian; 11-28-2007 at 09:47 AM.
- 11-28-2007, 09:45 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
It is always a wise idea to catch specific exceptions. Try the following:
Java Code:try { smtpSocket = new Socket("javabook", 22122); os = new DataOutputStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); }
- 11-28-2007, 09:48 AM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
Thanks javagk.
I tested it and it works perfectly fine.
- 11-29-2007, 09:56 AM #4
Member
- Join Date
- Nov 2007
- Posts
- 97
- Rep Power
- 0
Thanks guys. It was helpful.
Just checked the Java docs and it says:
Java Code:public Socket(InetAddress address, int port) Throws: IOException - if an I/O error occurs when creating the socket. SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
Similar Threads
-
Better way to handle exceptions
By javaplus in forum Advanced JavaReplies: 2Last Post: 01-16-2008, 06:47 PM -
Select specific cell
By Echilon in forum New To JavaReplies: 1Last Post: 01-01-2008, 07:47 AM -
Rejecting connections from a specific IP
By javaplus in forum NetworkingReplies: 2Last Post: 12-21-2007, 02:28 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks