2

I'm trying to understand some code written by someone else, and do not understand how 'throws' is used there. Here's the code:

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class WhoisQuery {

    public static void main(String[] args) throws Exception {
        String domainNameToCheck = "abcnews.com";
        performWhoisQuery("whois.enom.com", 43, domainNameToCheck);
        performWhoisQuery("whois.internic.net", 43, domainNameToCheck);
    }

    public static void performWhoisQuery(String host, int port, String query) throws Exception {
        System.out.println("**** Performing whois query for '" + query + "' at " + host + ":" + port);

        Socket socket = new Socket(host, port);

        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println(query);

        String line = "";
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }

As I understand, code with throws should be handled in some way. However, there's no handling mechanism like try catch. So what's the exact purpose of throws here? I tested it, and without throws it returned IOexception.

My questions are:

  1. Is throws part written correctly? I assume some handling mechanism should be here, but I am not confident.
  2. Why does the code work now, and does not without 'throws' in this state? Should it not return an error without catch/try statements? I do not see a difference between no 'throws' and throws without catch/try.
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Sam
  • 79
  • 6

4 Answers4

1

Anything thrown from main gets written to stderr. If you have a small program and you just want to quit processing and see the stacktrace on the console if there's an error, this is the easiest way to handle exceptions.

IOException is a checked exception, so you have to throw it if you want to avoid having to catch it in that method. IOException is a subclass of Exception, so throws Exception includes IOException.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
1

As you noted, "code with throws should be handled in some way". However, a try-catch clause isn't the only way to handle a throws declaration. Another option could be to decide you are not handling that exception, and simply add a throws clause to the calling method. You can continue adding these throws declarations until you get to main, in which case you're essentially declaring that your program doesn't handle errors, and will unceremoniously crash when such an error occurs.

It's not elegant or "proper", but hey, it works.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thanks, weird it took me so much time to find this information. (though technically I did not find it, I had it explained) – Sam Sep 24 '15 at 20:45
1

Some of the methods in the code you provided can throw checked exception. These checked exceptions must be handled by the code. And for this you have two options:

  1. Either handle possible exception with try/catch block
  2. Or throw exception from the method providing throws keyword in the method signature

As you can see, the author of the code decided to throw exception from the method without handling. Exception will go up to the main method where it will be printed to the error output before program is terminated.

sirdarpeace
  • 352
  • 3
  • 10
1

If you are referring to the throws in the main method, then its a way to catch any possible exception without having to use a catch block.

Essentially nothing has to catch it as java itself doesn't call main, the VM does, and if the VM catches an exception then it prints the stack trace to the stderr System.err.print () and then exits.

This is mostly useful when testing as you don't have to worry about catching and so on, and then just let the VM do everything itself.

Luke Melaia
  • 1,470
  • 14
  • 22