Skip to content

Instantly share code, notes, and snippets.

@atsushi-kitazawa
Created July 1, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atsushi-kitazawa/06229bce7aefd44edccdd62d7a56528f to your computer and use it in GitHub Desktop.
Save atsushi-kitazawa/06229bce7aefd44edccdd62d7a56528f to your computer and use it in GitHub Desktop.
How to throw exception class obtained by reflection
package com.example.reflection;
public class ExReflectionSample {
public static void main(String[] args) throws Exception {
Class<? extends Exception> clazz = (Class) Class.forName("com.example.reflection.MyException");
try {
throw clazz.getConstructor(String.class).newInstance("test");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.reflection;
public class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment