{"id":1364,"date":"2013-08-06T14:35:11","date_gmt":"2013-08-06T19:35:11","guid":{"rendered":"http:\/\/alejandroayala.solmedia.ec\/?p=1364"},"modified":"2013-08-06T14:39:10","modified_gmt":"2013-08-06T19:39:10","slug":"que-hay-dentro-de-un-invocationtargetexception","status":"publish","type":"post","link":"https:\/\/alejandroayala.solmedia.ec\/?p=1364","title":{"rendered":"\u00bfQu\u00e9 hay dentro de un InvocationTargetException?"},"content":{"rendered":"<p style=\"text-align: justify;\">Al usar librer\u00edas muchas de las veces tenemos que heredar una excepci\u00f3n del tipo <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/Throwable.html\" target=\"_blank\">InvocationTargetException<\/a> en mi caso al implementar una pantalla que procesaba los eventos con hilos para dar funcionalidad a una barra de progreso en <strong>Eclipse RCP 3.x<\/strong> el correr el hilo de la siguiente manera:<\/p>\n<p><!--more--><\/p>\n<p>[java]<br \/>\norg.eclipse.jface.wizard.Wizard.getContainer().run(true, false, operacion);<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify;\">La firma del m\u00e9todo me obligaba a capturar esta excepci\u00f3n, <strong>pero que es y como tratarla<\/strong>.<\/p>\n<p style=\"text-align: justify;\">De lo poco que encontr\u00e9 en la web esto es lo que pude aclarar, cualquier aclaraci\u00f3n nos vemos en los comentarios.<\/p>\n<p style=\"text-align: justify;\">Cualquier <strong><em>Throwable<\/em> <\/strong>se puede envolver en un <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/Throwable.html\" target=\"_blank\">java.lang.reflect.InvocationTargetException<\/a> como se aprecia en el constructor:<\/p>\n<p>[java]<br \/>\nInvocationTargetException(Throwable target)<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify;\">Incluso un valor nulo puede estar en su interior, el JavaDoc para el m\u00e9todo <em><strong>getCause()<\/strong><\/em> as\u00ed lo demuestra:<\/p>\n<blockquote><p><em>Returns the the cause of this exception (the thrown target exception, which may be null).<\/em><\/p><\/blockquote>\n<p style=\"text-align: justify;\">As\u00ed que cuando hacemos una llamada a <strong><em>InvocationTargetException.getCause()<\/em><\/strong> hay que tener en cuenta que puede devolver estas cuatro cosas:<\/p>\n<ol>\n<li><em>null<\/em><\/li>\n<li>Excepciones <em>unchecked<\/em> (<em>RuntimeExceptions<\/em> como <em>IllegalArgumentException<\/em>, <em>NullPointerException<\/em>, etc)<\/li>\n<li>Excepciones <em>checked<\/em> ( <em>NameNotFoundException<\/em>, <em>TuExcepcion<\/em>, etc)<\/li>\n<li><em>java.lang.Error<\/em> (<em>StackOverflowError<\/em>, <em>OutOfMemoryError<\/em>, <em>InternalError<\/em>, <em>UnknownError<\/em>)<\/li>\n<\/ol>\n<p>[java]<br \/>\n   miMetodo.invocar(Algo);<br \/>\n} catch (IllegalAccessException ex) {<br \/>\n   logger.log(Level.SEVERE, &quot;Invocation failed.&quot;, ex);<br \/>\n} catch (InvocationTargetException ex) {<br \/>\n   Throwable cause = ex.getCause();<br \/>\n   if (cause == null) {<br \/>\n      throw new IllegalStateException(<br \/>\n        &quot;InvocationTargetException, cause es nulo.&quot;, ex);<br \/>\n   } else if (cause instanceof RuntimeException) {<br \/>\n      throw (RuntimeException) cause;<br \/>\n   } else if (cause instanceof Exception) {<br \/>\n      logger.log(Level.SEVERE, &quot;Invocation fallo. Causa: &quot;, cause);<br \/>\n   } else {<br \/>\n      logger.log(Level.SEVERE, &quot;Invocation fallo. Error: &quot;, cause);<br \/>\n   }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify;\">Por lo tanto el siguiente c\u00f3digo puede producir un <strong><em>NullPointerException<\/em><\/strong>, mucha gente no revisa que el valor no apunte a un nulo.<\/p>\n<p>[java]<br \/>\n} catch (InvocationTargetException ex)<br \/>\n   Throwable cause = ex.getCause();<br \/>\n   cause.printStackTrace();<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify;\">El siguiente c\u00f3digo puede causar un <em><strong>ClassCastException<\/strong> <\/em>del casting de un <em>RuntimeException<\/em>, ya que la causa puede ser un <strong><em>Error<\/em><\/strong><\/p>\n<p>[java]<br \/>\n} catch (InvocationTargetException ex) {<br \/>\n   Throwable cause = ex.getCause();<br \/>\n   if(cause instanceof MyApplicationException) {<br \/>\n      \/\/codigo<br \/>\n   } else {<br \/>\n      \/\/no son excepciones de la aplicacion y se los pasa por fuera<br \/>\n      throw (RuntimeException) cause;<br \/>\n   }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"text-align: justify;\">Espero les haya sido de Utilidad.<\/p>\n<p>.<\/p>\n<p>Fuente: <a href=\"https:\/\/blogs.oracle.com\/chengfang\/entry\/p_what_s_inside_invocationtargetexception\" target=\"_blank\">https:\/\/blogs.oracle.com\/chengfang\/entry\/p_what_s_inside_invocationtargetexception<\/a><br \/>\n.<\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=a1ejo_ayala\" class=\"twitter-share-button\" data-size=\"large\">Twittear<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Al usar librer\u00edas muchas de las veces tenemos que heredar una excepci\u00f3n del tipo InvocationTargetException en mi caso al implementar una pantalla que procesaba los eventos con hilos para dar funcionalidad a una barra de progreso en Eclipse RCP 3.x el correr el hilo de la siguiente manera:<\/p>\n","protected":false},"author":1,"featured_media":690,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[265,385,387,506,390,388,389,386],"_links":{"self":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1364"}],"collection":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1364"}],"version-history":[{"count":10,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1364\/revisions"}],"predecessor-version":[{"id":1372,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/posts\/1364\/revisions\/1372"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=\/wp\/v2\/media\/690"}],"wp:attachment":[{"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alejandroayala.solmedia.ec\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}