简体   繁体   English

将异常转移到UI的最佳方法?

[英]Best way to transfer exceptions to UI?

What is the better way to handle exceptions if I need to transfer them to UI. 如果需要将异常转移到UI,有什么更好的处理异常的方法。 What I need to use: throwing Checked/Unchecked(which from them is better) through several methods to UI to show to user an appropriate message or return error code from method to method to UI or maybe something else? 我需要使用的方法:通过UI的几种方法将Checked / Unchecked(从它们中选出更好)扔给UI,以向用户显示适当的消息,或者将错误代码从方法之间返回给UI或其他方法?

UPDATED: 更新:

Does it good idea to use Android 's Handler to handle all checked/unchecked (maybe only on of them) exceptions that I don't know what to do with them in place where they appear? 使用Android的处理程序来处理我不知道如何处理它们出现的地方的所有选中/未选中(也许仅在其中)异常,这是个好主意吗?

for example 例如

... ...

catch(NUllPointerException){
UIExceptionHandler.sendEmptyMessage(...);
}

... ...

onHandleMessage(...){
//handle exception here - for example show toast 
}

?

Like Dan W said, you can create your own exception and catch it on your UI Layer. 就像Dan W所说的那样,您可以创建自己的异常并在UI层上捕获它。 Extending RunetimeException allows you to throw exception without having to specify them in your methods header. 扩展RunetimeException允许您抛出异常,而不必在方法标头中指定它们。

Another important thing to consider is the EDT on your UI Layer. 要考虑的另一重要事项是UI层上的EDT。 Since it's not possible to wrap all of your Event Dispatcher Thread exceptions in a single location in your UI Layer, you can create an UncaughtExceptionHandler and manage all of the UncaughtExceptions that triggers in your application from there. 由于不可能将所有的Event Dispatcher Thread异常包装在UI层的单个位置中,因此您可以创建UncaughtExceptionHandler并管理从那里触发应用程序的所有UncaughtExceptions。

From there you can easily Log and inform the user of the error that happened and show different messages based on the type of exception that was thrown and not caught. 从那里,您可以轻松地记录日志并通知用户发生的错误,并根据引发和未捕获的异常类型显示不同的消息。 That way, you'll be certain no errors goes by unnoticed. 这样,您可以确定没有错误被忽略。

This discussion goes over the basic of implementing the Handler. 该讨论超出了实现Handler的基础。 How can i catch Event Dispatch Thread (EDT) exceptions? 如何捕获事件调度线程(EDT)异常?

I would personally create a custom exception and throw that all the way to the UI layer. 我将亲自创建一个自定义异常,并将其扔到UI层。 That way, you're not throwing all exceptions. 这样,您就不会抛出所有异常。 You can catch all the other exceptions in the lower layers of your project, but when you're at those areas that need to be brought back up the to UI, you catch the original exception and re-throw your custom one. 您可以在项目的较低层中捕获所有其他异常,但是当您需要将这些区域带回到UI时,您可以捕获原始异常并重新抛出自定义异常。

This is what I do in my current projects. 这就是我在当前项目中所做的。 If there is is an exception I catch them and store meaningful text such as - UserInput is too short or number not allowed etc., I store them in an array called ErrorVO. 如果有例外,我会捕获它们并存储有意义的文本,例如-UserInput太短或不允许使用数字等,我将它们存储在名为ErrorVO的数组中。 I then display the error messages on the UI. 然后,我在UI上显示错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM