Created
July 1, 2020 14:10
-
-
Save atsushi-kitazawa/06229bce7aefd44edccdd62d7a56528f to your computer and use it in GitHub Desktop.
How to throw exception class obtained by reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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