java - 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 same code without

throws arithmeticexception 

do same, when use 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 integer number!");     e. printstacktrace();   }   catch (runtimeexception e) {     system.out.println("number big!");     e. printstacktrace();   } } 

could describe me if use of

    throws arithmeticexception 

has advantages in code.

i appreciate example of using keyword throws. thank much!

since arithmeticexception unchecked exception, listing in throws specification has no effect far compiler concerned.

nonetheless, think idea keep throws specification documentation purposes.

that said, arithmeticexception not right exception throwing when function called invalid argument. using illegalargumentexception more appropriate.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -