Results 1 to 4 of 4
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default TCPIP Client (handling specific exceptions)

    I have a TCPIP client with following code.

    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");
    	}
    I want to catch specific exceptions for Port and for hostname. Please advice.
    Last edited by ravian; 11-28-2007 at 09:47 AM.

  2. #2
    javagk is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    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");
    }

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks javagk.
    I tested it and it works perfectly fine.

  4. #4
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    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

  1. Better way to handle exceptions
    By javaplus in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2008, 06:47 PM
  2. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 07:47 AM
  3. Rejecting connections from a specific IP
    By javaplus in forum Networking
    Replies: 2
    Last Post: 12-21-2007, 02:28 PM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •