import
java.lang.reflect.Method;
import
java.lang.reflect.Type;
public
class
GFG {
class
GFGSampleClass {
String value;
public
void
setValue(String value)
throws
ClassNotFoundException,
ArrayIndexOutOfBoundsException,
ArithmeticException
{
this
.value = value;
}
}
public
static
void
main(String[] args)
{
try
{
Class classobj = GFGSampleClass.
class
;
Method[] methods = classobj.getMethods();
Method method =
null
;
for
(Method m : methods) {
if
(m.getName().equals("setValue"))
method = m;
}
Type arithmeticExClassobj = ArithmeticException.
class
;
boolean
response = isCertainExceptionIsThrown(method,
arithmeticExClassobj);
System.out.println("ArithmeticException"
+ " is thrown by setValue(): " + response);
Type exceptionObj = IndexOutOfBoundsException.
class
;
response = isCertainExceptionIsThrown(method,
exceptionObj);
System.out.println("IndexOutOfBoundsException"
+ " is thrown by setValue(): " + response);
}
catch
(Exception e) {
e.printStackTrace();
}
}
private
static
boolean
isCertainExceptionIsThrown(Method method, Type exceptionName)
{
Type exceptions[] = method.getGenericExceptionTypes();
for
(
int
i =
0
; i < exceptions.length; i++) {
if
(exceptions[i] == exceptionName) {
return
true
;
}
}
return
false
;
}
}