java - how come the IOException isn't working but goes to the InputMismatchException -


i don't understand why simple method won't catch exception throws exception. exception error pops java.util.inputmismatchexception.

what can catches ioexception?

public class throwing {      public static void main(string[] args) {         try {             int x = getint();             system.out.println(x);         } catch (ioexception a) {             a.printstacktrace();         }     }      public static int getint() throws ioexception {         int = 0;         system.out.println("enter interger: ");         scanner input = new scanner(system.in);         = input.nextint();         return a;     } } 

if have @ javadocs scanner#nextint see it's capable of throwing inputmismatchexception, among others

but goal i'm trying throw ioexception rather inputmismatch. there can code me throw it?

you need trap possible exceptions , throw new ioexception old exception cause.

public static int getint() throws ioexception {     int = 0;     system.out.println("enter interger: ");     scanner input = new scanner(system.in);     try {         = input.nextint();     } catch (inputmismatchexception ime) {         throw new ioexception("could not int input", ime);     }     return a; } 

this is, imho, not brilliant idea, may wish treat inputmismatchexception differently ioexception might caused when create scanner


Comments