请提供示例代码或项目源代码我可以处理 HttpConnection 或类似的例外类型。
解决方案处理异常取决于异常。如果异常是您无法恢复的,用户需要知道的话,您可以捕获异常并将其显示在AlertDialog中:
try { // do something } catch(SomeImportantException e){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(用户友好的文本解释出了什么问题); AlertDialog alert = builder.create(); alert.show(); }有关对话框的更多信息,请参阅创建对话框。
或者,如果例外是你可以处理的事情,你可以只记录关于异常的信息并继续前进。
try { // do something } catch(SomeLessImportantException e){ Log.d(tag,Failed to do something:+ e.getMessage()); }
I am new to Android mobile application development. I would like to know, how can I handle exceptions like HttpConnection related exceptions or any other exceptions? Do I need to display an AlertDialog to the user?
Kindly provide a sample code or project source code on how can I handle HttpConnection or similar type of Exceptions.
解决方案How you handle exception depends on the exception. If the exception is something that you cannot recover from, and the user needs to know about then you could catch the exception and show it in an AlertDialog:
try { // do something } catch (SomeImportantException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("User friendly text explaining what went wrong."); AlertDialog alert = builder.create(); alert.show(); }For more info on the dialog, see creating dialogs.
Alternatively, if the exception is something that you can deal with, you can just log information about the exception and move on.
try { // do something } catch (SomeLessImportantException e) { Log.d(tag, "Failed to do something: " + e.getMessage()); }
更多推荐
用于处理异常的示例代码
发布评论