title: Parametrized Method in Java: try-catch does not catch ClassCastException (on Android)
body: <p>Consider creating a new Object and casting it to String within a try-catch block that catches an Exception. Everything seems to be ok. But what does happen if we cast an Object to T within similar try-catch block in a parametrized function? The exception is not catched and the program crashes raising a ClassCastException.</p>
  
  <p>Upd: thanks for answers and comments. But just to clarify: I understand why the exception is risen (because of casting Object to String obviously).</p>
  
  <p><strong>The question is</strong>: why does it NOT catch the ClassCastException during runtime in the method body?</p>
  
  <pre><code>class test
  {
      public void doTests()
      {
          Log.i("", "====== CLASS CAST TEST ======");
          Log.i("", "====== REGULAR CAST TEST ");
          final String test1 = test.regularCastTest(); // the exception thrown is catched within the     method
          Log.i("", "====== PARAMETRIZED CAST TEST ");
          final String test2 = test.&lt;String&gt;parametrizedCastTest(); // fails here with     ClassCastException without catching it
          Log.i("", "------ CLASS CAST TEST END --");
      }   
      static &lt;T&gt; T parametrizedCastTest()
      {
          Object obj = new Object();
          try
          {
              final T ret = (T) obj;
              Log.d("", String.format("%s; %s",     obj.getClass().getSimpleName(), ret.getClass().getSimpleName())); // does not fail and prints 'Object; Object'
              return ret;
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
  
          return null;
      }
  
      static String regularCastTest()
      {
          Object obj = new Object();
          try
          {
              final String ret = (String) obj;
              return ret;
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
          return "";
      }
  }
  </code></pre>
  
  <p>PS: the parametrized method does not fail if we simply call it without getting the result like:</p>
  
  <pre><code>test.&lt;String&gt;parametrizedCastTest();
  </code></pre>
  
  <p><strong>LOGCAT</strong></p>
  
  <pre><code>11-26 19:13:37.692: I/(13018): ====== CLASS CAST TEST ======
  11-26 19:13:37.692: I/(13018): ====== REGULAR CAST TEST 
  11-26 19:13:37.692: W/System.err(13018): java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
  11-26 19:13:37.692: W/System.err(13018):    at com.kaypu.goods.test.regularCastTest(test.java:57)
  11-26 19:13:37.692: W/System.err(13018):    at com.kaypu.goods.test.go1(test.java:30)
  11-26 19:13:37.692: W/System.err(13018):    at com.kaypu.goods.AppGoods.doTests(AppGoods.java:285)
  11-26 19:13:37.692: W/System.err(13018):    at com.kaypu.goods.AppGoods.onCreate(AppGoods.java:188)
  11-26 19:13:37.692: W/System.err(13018):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
  11-26 19:13:37.692: W/System.err(13018):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4473)
  11-26 19:13:37.692: W/System.err(13018):    at android.app.ActivityThread.access$1500(ActivityThread.java:144)
  11-26 19:13:37.692: W/System.err(13018):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
  11-26 19:13:37.692: W/System.err(13018):    at android.os.Handler.dispatchMessage(Handler.java:102)
  11-26 19:13:37.692: W/System.err(13018):    at android.os.Looper.loop(Looper.java:136)
  11-26 19:13:37.692: W/System.err(13018):    at android.app.ActivityThread.main(ActivityThread.java:5146)
  11-26 19:13:37.692: W/System.err(13018):    at java.lang.reflect.Method.invokeNative(Native Method)
  11-26 19:13:37.692: W/System.err(13018):    at java.lang.reflect.Method.invoke(Method.java:515)
  11-26 19:13:37.692: W/System.err(13018):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
  11-26 19:13:37.692: W/System.err(13018):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
  11-26 19:13:37.692: W/System.err(13018):    at dalvik.system.NativeStart.main(Native Method)
  11-26 19:13:37.692: I/(13018): ====== PARAMETRIZED CAST TEST 
  11-26 19:13:37.692: D/(13018): Object; Object
  11-26 19:13:37.692: D/AndroidRuntime(13018): Shutting down VM
  11-26 19:13:37.692: W/dalvikvm(13018): threadid=1: thread exiting with uncaught exception (group=0x415a2ce0)
  11-26 19:13:37.692: E/AndroidRuntime(13018): FATAL EXCEPTION: main
  11-26 19:13:37.692: E/AndroidRuntime(13018): Process: com.kaypu.goods, PID: 13018
  11-26 19:13:37.692: E/AndroidRuntime(13018): java.lang.RuntimeException: Unable to create application com.kaypu.goods.AppGoods: java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4476)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.app.ActivityThread.access$1500(ActivityThread.java:144)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.os.Handler.dispatchMessage(Handler.java:102)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.os.Looper.loop(Looper.java:136)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.app.ActivityThread.main(ActivityThread.java:5146)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at java.lang.reflect.Method.invokeNative(Native Method)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at java.lang.reflect.Method.invoke(Method.java:515)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at dalvik.system.NativeStart.main(Native Method)
  11-26 19:13:37.692: E/AndroidRuntime(13018): Caused by: java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at com.kaypu.goods.test.go1(test.java:32)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at com.kaypu.goods.AppGoods.doTests(AppGoods.java:285)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at com.kaypu.goods.AppGoods.onCreate(AppGoods.java:188)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4473)
  11-26 19:13:37.692: E/AndroidRuntime(13018):    ... 10 more
  </code></pre>