Skip to content

Instantly share code, notes, and snippets.

@Chris-Gillis
Created August 9, 2013 02:42
Show Gist options
  • Save Chris-Gillis/6190773 to your computer and use it in GitHub Desktop.
Save Chris-Gillis/6190773 to your computer and use it in GitHub Desktop.
Good use of a try-catch
try
{
// Some code that can fail
}
catch(NumberFormatException e)
{
Log.v(TAG, "User entered invalid data: carbs - " + carbsString + " fiberString - " + fiberString);
// I feel dirty for this hack
mCarbsAmountEditText.removeTextChangedListener(mCarbsTextWatcher);
mCarbsAmountEditText.setText("");
mCarbsAmountEditText.addTextChangedListener(mCarbsTextWatcher);
mFiberAmountEditText.removeTextChangedListener(mFiberTextWatcher);
mFiberAmountEditText.setText("");
mFiberAmountEditText.addTextChangedListener(mFiberTextWatcher);
updateNetCarbs();
View toastLayout = getLayoutInflater().inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView)toastLayout.findViewById(R.id.toastTextView);
text.setText(R.string.toastErrorMessage);
Toast errorToast = new Toast(getApplicationContext());
errorToas t.setGravity(Gravity.TOP, 0, 0);
errorToast.setDuration(Toast.LENGTH_LONG);
errorToast.setView(toastLayout);
errorToast.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment