Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using keyword throws to declare throwing of Runtime Exceptions

For me, these code

static int faktorial (int n) throws ArithmeticException {
  if ((n < 0) || (n > 31)) { 
    throw new ArithmeticException();
  } 
  if (n > 1) {
    return n * faktorial(n - 1);
  } 
  else {
    return 1;
  }    
}

and the same code without

throws ArithmeticException

do the same, when I use it with the following code:

public static void main(String[] args) {
  try {
    int n;
    Scanner sc = new Scanner(System.in);  

    System.out.print("Insert integer number: ");        
    n = sc.nextInt();         
    System.out.println(n + "! = " + faktorial(n));
  }
  catch (InputMismatchException e) {
    System.out.println("Not an integer number!");
    e. printStackTrace();
  }
  catch (RuntimeException e) {
    System.out.println("Number is too big!");
    e. printStackTrace();
  }
}

Could someone describe me if use of

    throws ArithmeticException

has some advantages in my code.

I will also appreciate some good example of using keyword throws. Thank you very much!

like image 383
mcihak Avatar asked Apr 01 '13 13:04

mcihak


4 Answers

Since ArithmeticException is an unchecked exception, listing it in the throws specification has no effect as far as the compiler is concerned.

Nonetheless, I think it is a good idea to keep the throws specification for documentation purposes.

That said, ArithmeticException is probably not the right exception to be throwing when the function is called with an invalid argument. Using IllegalArgumentException would be more appropriate.

like image 137
NPE Avatar answered Sep 28 '22 05:09

NPE


Aside of what's beeing said, there is one upside that I find helping me code.

Eclipse can use that information (that the method is declared with throws RuntimeException) and (on demand) add proper catch clause.

Lets look at this:

public static void main(String[] args) {
    test();
}

private static void test() {
    foo();
    bar();
    baz();
}

public static void foo() {

}

public static void bar() throws NullPointerException {

}

public static void baz() throws IllegalArgumentException {

}

adding try/ catch block inside method test will result in:

private static void test() {
    try {
        foo();
        bar();
        baz();
    } catch (NullPointerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

That's nice, isn't it? However, it works only with 1st level method invocation, so it does not pollute higher levels of abstraction with multiple catches of runtime exceptions:

public static void main(String[] args) {
    try {
        test();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static void test() {
    foo();
    bar();
    baz();
}

That's nice as well.

Anyway, seeing that throws in javadoc is probably much more important :)

void Test.baz() throws IllegalArgumentException
like image 37
dantuch Avatar answered Sep 28 '22 04:09

dantuch


The above method signature uses the the expression throws ArithmeticException to tell the user of this function that method could throw this exception and they should be aware and catch it (if they want - "want" only because this exception is unchecked - and plan a workaround in that case).

Except that ArithmeticException is not the proper exception to use in this case (you could technically use any one of them). A better exception to throw would be IllegalArgumentException since it implies the argument that was passed in was improper.

like image 31
jn1kk Avatar answered Sep 28 '22 04:09

jn1kk


Since Java does not provide a simple True/False flag to determine whether a given exception is checked or unchecked, you need to be aware of what types of exceptions you (as programmer) has to deal with (either catch or re-throw) and which you can chose not to handle (effectively ignore).

In particular, the entire RunTimeException class is unchecked, and ArithmeticException is its sub-class.

like image 25
PM 77-1 Avatar answered Sep 28 '22 05:09

PM 77-1