0

Lets say we have two classes Connector and Main. First one has method connect(); and another has actionListener() at the button. The Connector.connect() may throw an exception. I'm not sure where is the proper place to handle this; in Connector or in Main in general? If we programming android, is it the same place to put logs ?

Edit: Great article covering above problem: https://today.java.net/pub/a/today/2003/12/04/exceptions.html

1
  • Throw it or re-throw as a custom exception from Connector to Main. Logging is optional and useful thing. Commented Oct 25, 2013 at 12:16

2 Answers 2

2

It would be much better to throw the exception to the user level if you want the user to know about the exact exception. Otherwise if it is not that important and the exception can be recovered then you can handle it in Connector itself and return some value representing error.

If the exception being thrown from the Connector is not inline with the abstraction level of Main method, then wrap it around an exception that is inline with the caller's abstraction level.

Throw early and catch late is the general rule for exception handling.

Sign up to request clarification or add additional context in comments.

Comments

0

Depends on the kind of actions of the handler. If you need to show a message to the user, handle the exception in the view layer (guess it´s Connector in your case). If you need to do actions with the database (for example, a rollbak), handle it in the persistence layer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.