Currently creating an Android app. I have a class called Pollen
, on top of my Fragment
class, and I am dealing with exception handling within this second class.
Here is my Pollen class.
public static class Pollen
{
@SuppressLint("SimpleDateFormat")
public Pollen(int zipcode, Context context)
{
this.context = context;
this.zipcode = zipcode;
Document doc;
try
{
// pass address to
doc = Jsoup.connect("http://www.wunderground.com/DisplayPollen.asp?Zipcode=" + this.zipcode).get();
// get "location" from XML
Element location = doc.select("div.columns").first();
this.location = location.text();
// get "pollen type" from XML
Element pollenType = doc.select("div.panel h3").first();
this.pollenType = pollenType.text();
SimpleDateFormat format = new SimpleDateFormat("EEE MMMM dd, yyyy");
// add the four items of pollen and dates
// to its respective list
for(int i = 0; i < 4; i++)
{
Element dates = doc.select("td.text-center.even-four").get(i);
Element levels = doc.select("td.levels").get(i);
try
{
pollenMap.put(format.parse(dates.text()), levels.text());
}
catch (ParseException e)
{
Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
toast.show();
return;
}
}
}
catch (IOException e)
{
Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
toast.show();
return;
}
}
}
Since Pollen
uses Network tasks, it's under an asynchronous thread which is irrelevant to this question.
Whenever Pollen
is met under an exception, despite the Toast
, the app would crash.
I was wondering - what is the accepted way to deal with exception on classes outside of the main class?