无法捕获NumberFormatException

时间:2014-09-30 21:42:40

标签: java android exception-handling try-catch

我在Logcat输出中得到一个NumberFormatException,我想压制它。显然这个例外只发生在华为设备上(我碰巧拥有)。由于某种原因,我的catch()块没有执行。有人可以帮忙:

具体来说,我想知道为什么我的catch()代码没有被调用。

Locale locale = Locale.getDefault();
System.out.println("Locale is  : [" + locale + "]"); // make sure there is a default Locale

 System.out.printf( "==Before try{}\n" );

try
{
  System.out.printf( "---Line44\n" );
  Calendar c = Calendar.getInstance(locale);
  System.out.printf( "---Line46\n" );
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  System.out.printf( "---Line48\n" );
  String formattedDate = df.format(c.getTime());
  System.out.printf( "---Line50\n" );
  System.out.printf( "Timestamp  : %s\n", formattedDate );
  System.out.printf( "---Line52\n" );
}
catch( java.lang.NumberFormatException e )
{
  // Ignore stupid exception - Huawei specific apparently..
  System.out.printf( "HUAWEI bug\n" );
}

System.out.printf( "==After Entire Block, Line60\n" );

有关更多信息,请提及here华为错误,但我的try / catch语句肯定有问题吗?

这是异常转储:

10-01 11:07:53.402: I/System.out(1758): ==Before try{}
10-01 11:07:53.402: I/System.out(1758): ---Line44
10-01 11:07:53.432: W/System.err(1758): java.lang.NumberFormatException: Invalid int: ""
10-01 11:07:53.432: W/System.err(1758):     at java.lang.Integer.invalidInt(Integer.java:138)
10-01 11:07:53.432: W/System.err(1758):     at java.lang.Integer.parseInt(Integer.java:359)
10-01 11:07:53.432: W/System.err(1758):     at java.lang.Integer.parseInt(Integer.java:332)
10-01 11:07:53.432: W/System.err(1758):     at java.util.Calendar.getHwFirstDayOfWeek(Calendar.java:807)
10-01 11:07:53.432: W/System.err(1758):     at java.util.Calendar.<init>(Calendar.java:745)
10-01 11:07:53.432: W/System.err(1758):     at java.util.GregorianCalendar.<init>(GregorianCalendar.java:338)
10-01 11:07:53.442: W/System.err(1758):     at java.util.GregorianCalendar.<init>(GregorianCalendar.java:314)
10-01 11:07:53.442: W/System.err(1758):     at java.util.Calendar.getInstance(Calendar.java:1098)
10-01 11:07:53.442: W/System.err(1758):     at com.example.test.MyApplication.onCreate(MyApplication.java:45)
10-01 11:07:53.442: W/System.err(1758):     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
10-01 11:07:53.442: W/System.err(1758):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3954)
10-01 11:07:53.442: W/System.err(1758):     at android.app.ActivityThread.access$1300(ActivityThread.java:123)
10-01 11:07:53.442: W/System.err(1758):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
10-01 11:07:53.442: W/System.err(1758):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 11:07:53.442: W/System.err(1758):     at android.os.Looper.loop(Looper.java:137)
10-01 11:07:53.442: W/System.err(1758):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-01 11:07:53.442: W/System.err(1758):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 11:07:53.442: W/System.err(1758):     at java.lang.reflect.Method.invoke(Method.java:511)
10-01 11:07:53.442: W/System.err(1758):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-01 11:07:53.442: W/System.err(1758):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-01 11:07:53.442: W/System.err(1758):     at dalvik.system.NativeStart.main(Native Method)
10-01 11:07:53.442: I/System.out(1758): ---Line46
10-01 11:07:53.452: I/System.out(1758): ---Line48
10-01 11:07:53.462: I/System.out(1758): ---Line50
10-01 11:07:53.462: I/System.out(1758): Timestamp  : 2014-10-01 11:07:53
10-01 11:07:53.462: I/System.out(1758): ---Line52
10-01 11:07:53.462: I/System.out(1758): ==After Entire Block, Line61

1 个答案:

答案 0 :(得分:2)

我会检查调用堆栈中其他项的来源。似乎有可能其他东西正在捕获并记录并返回而没有代码的异常。时间戳行是否显示在输出中?