Java Java Data Structures Efficiency! Design the UI

How do you decide where is a good place to catch an exception?

When Craig used the "readLine()" method he said that he didn't want to catch it in the same method but bubble up the exception instead, it wasn't clear to me why it was done this way.

1 Answer

Sławomir Lasik
Sławomir Lasik
7,792 Points

Well think about it.

Let's assume that he catches it when he uses readLine() method. This is a private method. So the programmer who is using this class doesn't even know about this method. And how it works. And that it handles exceptions. But he is aware that it might go something wrong in giving input. He is not able to handle it how he wants to. Because it is done already in this private method. And what if the situation when the exception is thrown is not handled how the programmer using this class want to handle.

Its a really design issue I think. Its up to the user of the class (or method) how he handles the exceptions in this situation.