Close

Java Reflection - Method.getExceptionTypes() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public abstract Class<?>[] getExceptionTypes()

Returns an array of Class objects that represent the types of exceptions declared to be thrown by the method


Examples


package com.logicbig.example.method;

import java.lang.reflect.Method;
import java.util.Arrays;

public class GetExceptionTypesExample {

public static void main(String... args) throws NoSuchMethodException {
Method m = GetExceptionTypesExample.class.getDeclaredMethod("main", String[].class);
Class<?>[] c = m.getExceptionTypes();
System.out.println(Arrays.toString(c));
}
}

Output

[class java.lang.NoSuchMethodException]




package com.logicbig.example.method;

import java.io.IOException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Arrays;

public class GetExceptionTypesExample2 {

private static class Processor {
private void process() throws IOException, ParseException {}
}

public static void main(String... args) throws NoSuchMethodException {
Method m = Processor.class.getDeclaredMethod("process");
Class<?>[] exceptionTypes = m.getExceptionTypes();
System.out.println(Arrays.toString(exceptionTypes));

}
}

Output

[class java.io.IOException, class java.text.ParseException]




See Also