【发布时间】:2014-04-24 23:48:36
【问题描述】:
我有一个方法,假设去获取一个 JSON 对象,该对象包含英雄联盟的所有冠军 ID,我希望它根据传递给方法的“数字”返回冠军的名称程序中的另一个循环。
public String getChampionName(int number) //where it is saying its not returning a string
{
try
{
String JSonChampionName = readURL("myURLwithAPIkey");
JSONObject object = JSONObject.fromObject(JSonChampionName);
JSONObject championData = (JSONObject)(object.get("data"));
JSONObject champName = (JSONObject)(championData.get(number));
if(object != null && championData != null && champName != null)
{
String cName = champName.get("name").toString();
return cName;
}
else
return "";
}catch(Exception v){}
}
任何想法,我只是不确定为什么它告诉我该方法没有返回字符串。
【问题讨论】:
-
如果抛出异常,这个方法会发生什么?然后不会返回任何字符串。这就是它抱怨的原因。
-
如果一个异常被抛出并且你捕获了它......你到底返回了什么?
-
另外:当您丢弃异常时,您可能需要重新考虑程序的逻辑。