views:

62

answers:

2

I noticed that a toast isn't displayed when it's used inside a catch block. Does anyone know how to show toasts when catching exceptions? An Example:

try {
    // try to open a file
} catch (FileNotFoundException e) {
    Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG);
    return; // cancel processing
}
+4  A: 

Should be like this:

Toast toast = Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG);
toast.show();
Peter Knego
Hah, that was blindingly obvious. Sometimes we look for the complications in the sea of simplicity. :)
JimR
Oops.. alright ;-) thanks!
cody
+1  A: 

Yes, I put it right behind the existing line:

Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG).show();
cody