0

What is the better way to handle exceptions if I need to transfer them to 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?

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?

for example

...

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

...

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

?

drifter
  • 683
  • 1
  • 11
  • 24

3 Answers3

5

Like Dan W said, you can create your own exception and catch it on your UI Layer. Extending RunetimeException allows you to throw exception without having to specify them in your methods header.

Another important thing to consider is the EDT on your UI Layer. 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.

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. How can i catch Event Dispatch Thread (EDT) exceptions?

Community
  • 1
  • 1
Alexandre
  • 4,382
  • 2
  • 23
  • 33
3

I would personally create a custom exception and throw that all the way to the UI layer. 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.

Dan W
  • 5,718
  • 4
  • 33
  • 44
-1

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. I then display the error messages on the UI.

Dinesh Arora
  • 2,115
  • 3
  • 24
  • 30
  • How do you inform UI that error appear and get appropriate value from array? – drifter Jan 26 '12 at 21:10
  • You send them as JSON object or in my current application, I use velocity and send back the error object as list of string and iterate the text. – Dinesh Arora Feb 29 '12 at 01:55