Catch not working, why?
-
When the following code runs, an exception occurs at the setImageResource line. When I debug it, the right after that line the debugger jumps to the finally part, and then it goes into some java code in Runtimeinit.java to a method called private static class UncaughtHandler implements Thread.UncaughtExceptionHandler If I remark the finally section, then the exception is not caught by the catch part? Why isn't the catch part working? Isn't "catch (Exception ex)" supposed to catch any type of exception?
try {
ImageView imageV = (ImageView) findViewById(R.id.imageView);Log.i(TAG, "before setImageResource"); imageV.setImageResource(R.drawable.merle);
} catch (Exception ex) {
Log.e(TAG,ex.getMessage().toString());
} finally {
Log.i(TAG,"finally");
} -
When the following code runs, an exception occurs at the setImageResource line. When I debug it, the right after that line the debugger jumps to the finally part, and then it goes into some java code in Runtimeinit.java to a method called private static class UncaughtHandler implements Thread.UncaughtExceptionHandler If I remark the finally section, then the exception is not caught by the catch part? Why isn't the catch part working? Isn't "catch (Exception ex)" supposed to catch any type of exception?
try {
ImageView imageV = (ImageView) findViewById(R.id.imageView);Log.i(TAG, "before setImageResource"); imageV.setImageResource(R.drawable.merle);
} catch (Exception ex) {
Log.e(TAG,ex.getMessage().toString());
} finally {
Log.i(TAG,"finally");
}Solved by adding a catch for Throwable try { ImageView imageV = (ImageView) findViewById(R.id.imageView); Log.i(TAG, "before setImageResource"); imageV.setImageResource(R.drawable.merle); } catch (Exception ex) { Log.e(TAG,ex.getMessage().toString()); } catch (Throwable ex) { Log.e(TAG,ex.getMessage().toString()); } finally { Log.i(TAG,"finally"); }
-
When the following code runs, an exception occurs at the setImageResource line. When I debug it, the right after that line the debugger jumps to the finally part, and then it goes into some java code in Runtimeinit.java to a method called private static class UncaughtHandler implements Thread.UncaughtExceptionHandler If I remark the finally section, then the exception is not caught by the catch part? Why isn't the catch part working? Isn't "catch (Exception ex)" supposed to catch any type of exception?
try {
ImageView imageV = (ImageView) findViewById(R.id.imageView);Log.i(TAG, "before setImageResource"); imageV.setImageResource(R.drawable.merle);
} catch (Exception ex) {
Log.e(TAG,ex.getMessage().toString());
} finally {
Log.i(TAG,"finally");
}But why u put this code in try, catch block. Check your layout that present this imageview which id is R.id.imageView.
-
When the following code runs, an exception occurs at the setImageResource line. When I debug it, the right after that line the debugger jumps to the finally part, and then it goes into some java code in Runtimeinit.java to a method called private static class UncaughtHandler implements Thread.UncaughtExceptionHandler If I remark the finally section, then the exception is not caught by the catch part? Why isn't the catch part working? Isn't "catch (Exception ex)" supposed to catch any type of exception?
try {
ImageView imageV = (ImageView) findViewById(R.id.imageView);Log.i(TAG, "before setImageResource"); imageV.setImageResource(R.drawable.merle);
} catch (Exception ex) {
Log.e(TAG,ex.getMessage().toString());
} finally {
Log.i(TAG,"finally");
}But why you put this code on try and catch block. Firat see your layout and check that the imageview with id R.id.imageView is contain or not.
-
But why you put this code on try and catch block. Firat see your layout and check that the imageview with id R.id.imageView is contain or not.
It was just a test code. I was more interested in understanding why the try/catch did not work. The imageView was less of a concern in this excercise. Once I was able to trap the error , it turned out to be "not enough memory" because the bitmap I was using was too big.