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