InvocationTargetException of Java exception Handling (reflection exception)

Source: Internet
Author: User

InvocationTargetException of Java exception Handling (reflection exception)

InvocationTargetException exception by Method.invoke (obj, args ...) Method is thrown. This exception is received when an exception is thrown inside the called method and is not captured!!!

Example:

  1. Package com.zzj.test.reflect;
  2. Public class Reflect {
  3. public void Run (int i) throws zeroexception {
  4. b b = new B ();
  5. B.run (i);
  6. }
  7. }
  8. Class B {
  9. public void Run (int i) throws zeroexception {
  10. if (I < 0) {
  11. throw New Zeroexception ("parameter cannot be less than 0!  ");
  12. }
  13. SYSTEM.OUT.PRINTLN ("parameter:" + i);
  14. }
  15. }
  16. Class Zeroexception extends Exception {
  17. private Static final long serialversionuid = 1L;
  18. private String Detailmessage;
  19. Public zeroexception (String detailmessage) {
  20. this.detailmessage = detailmessage;
  21. }
  22. Public String GetMessage () {
  23. return detailmessage;
  24. }
  25. }

Test:

  1. Package com.zzj.test.reflect;
  2. Import java.lang.reflect.InvocationTargetException;
  3. Import Java.lang.reflect.Method;
  4. Public class Test {
  5. public static void Main (string[] args) {
  6. try {
  7. Class<?> clazz = Class.forName ("Com.zzj.test.reflect.Reflect");
  8. method = Clazz.getmethod ("Run", Int.Class);
  9. Method.invoke (Clazz.newinstance (),-1);
  10. } catch (ClassNotFoundException e) {
  11. E.printstacktrace ();
  12. } catch (SecurityException e) {
  13. E.printstacktrace ();
  14. } catch (Nosuchmethodexception e) {
  15. E.printstacktrace ();
  16. } catch (IllegalArgumentException e) {
  17. E.printstacktrace ();
  18. } catch (Illegalaccessexception e) {
  19. E.printstacktrace ();
  20. } catch (InvocationTargetException e) {
  21. System.out.println ("receive exceptions that are not caught inside the called method here");
  22. E.printstacktrace ();
  23. } catch (Instantiationexception e) {
  24. E.printstacktrace ();
  25. }
  26. }
  27. }

Output:

  1. The exception that is not caught inside the called method is received here
  2. Java.lang.reflect.InvocationTargetException
  3. At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
  4. At Sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
  5. At Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
  6. At Java.lang.reflect.Method.invoke (Unknown Source)
  7. At Com.zzj.test.reflect.Test.main (test.java:11)
  8. Caused by:com.zzj.test.reflect.ZeroException: parameter cannot be less than 0!
  9. At Com.zzj.test.reflect.b.run (reflect.java:13)
  10. At Com.zzj.test.reflect.Reflect.run (reflect.java:6)
  11. ... 5 more

You can also print the target exception directly:

  1. Package com.zzj.test.reflect;
  2. Import java.lang.reflect.InvocationTargetException;
  3. Import Java.lang.reflect.Method;
  4. Public class Test {
  5. public static void Main (string[] args) {
  6. try {
  7. Class<?> clazz = Class.forName ("Com.zzj.test.reflect.Reflect");
  8. method = Clazz.getmethod ("Run", Int.Class);
  9. Method.invoke (Clazz.newinstance (),-1);
  10. } catch (ClassNotFoundException e) {
  11. E.printstacktrace ();
  12. } catch (SecurityException e) {
  13. E.printstacktrace ();
  14. } catch (Nosuchmethodexception e) {
  15. E.printstacktrace ();
  16. } catch (IllegalArgumentException e) {
  17. E.printstacktrace ();
  18. } catch (Illegalaccessexception e) {
  19. E.printstacktrace ();
  20. } catch (InvocationTargetException e) {
  21. System.out.println ("receive exceptions that are not caught inside the called method here");
  22. Throwable t = e.gettargetexception (); //Get Target exception
  23. T.printstacktrace ();
  24. } catch (Instantiationexception e) {
  25. E.printstacktrace ();
  26. }
  27. }
  28. }

Output:

    1. here receives an exception that is not caught inside the called method   
    2. com.zzj.test.reflect.zeroexception:  Parameter cannot be less than 0!   
    3.     at com.zzj.test.reflect.b.run (Reflect.java:13)   
    4.     at com.zzj.test.reflect.reflect.run (Reflect.java:6)    
    5.     at sun.reflect.nativemethodaccessorimpl.invoke0 (Native  method)   
    6.     at sun.reflect.nativemethodaccessorimpl.invoke ( Unknown source)   
    7.     at  Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source)   
    8.      at java.lang.reflect.method.invoke (Unknown source)   
    9.      at com.zzj.test.reflect.test.main (test.java:11)   

InvocationTargetException of Java Exception Handling (reflection exception)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.