java - How to check type of variable in Exception? -
i created own exception in program show error
class invalidtypeexception extends exception { invalidtypeexception(string s) { super(s); } } public class customexception1 { static void valid(int a) throws invalidtypeexception { if (a instanceof integer) throw new invalidtypeexception("valid"); else system.out.println("invalid"); } public static void main(string args[]) { try { valid(12); } catch (exception e) { system.out.println(e); } } }
the compilation error is:
src\customexception1.java:11: error: unexpected type if(a instanceof integer ) ^ required: reference found: int 1 error
you can't use instanceof
operator on primitive types, a
defined. define argument type object
:
static void valid(object o) throws invalidtypeexception { if (o instanceof integer) { throw new invalidtypeexception("valid"); } else { system.out.println("invalid"); } }
Comments
Post a Comment