Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java: Why must one explicitly try a method call to catch throws?

I understand throwing an exception and I understand the need to catch it. What I don't understand is the need to explicitly "try" a method call to look for the error- shouldn't all input be "tried" to screen for exceptions?

Is this a debugging tool, or is try/catch always present in final code? Should all user input occur inside a try/catch block?

Thanks!!

3 Answers

Please re-phrase the question

What I don't understand is the need to explicitly "try" a method call to look for the error- shouldn't all input be "tried" to screen for exceptions?

Why all input should be tried for exception?

If you use method that does not throw any Exception, why do we need to try it ?

In fact we should not, because that will be misleading for other programmers reading your code.

Exception are used to track specific errors: wrong input, null pointer, and etc.

Exception is the way to change unwanted behavior.

I don't know how to explain, but simply don't, just don't put all your code in a try catch block

Its like in order to exterminate a bug, you blow up the house where he is into.

I don't know better analogy.

Again if some method throws SomeException

Then when you use it in your method, you either have to try .. catch block around the place where the method was executed, or declare your method with throwing method as throws Exception and catch error somewhere later.

"Exception are used to track specific errors: wrong input, null pointer, and etc."

So, if I understand this correctly, you have to think of every possible error that can occur and include a throw to catch each one, AND include a try/catch block in the executable at every point where each thrown exception might occur?

Sounds right with one correction.

Try/catch block comes usually at the very top of application, as close to user as possible.

On other layers you just add throws Exception

Let me try to explain you on concrete example 'Java Spring Framework' and AccessDeniedException.

So in a good Web Java App you have following layers:

  • database layer (where database is changed)
  • service layer (where other not related changes happen with objects, before going to database)
  • controller layer (where actual requests to web pages are made)

So in sudo code you write a method that throws AccessDeniedException at database layer, then in Service layer you use another method that uses method database layer and add throws AccessDeniedException. And only at the top : at Controller layer, you write so-called Exception handler, that will deal with exception...

What i wanted to show with this example, that properly designed App, will not require you to write many try...catch blocks.

Mostly you will just add throws Exception to indicate that method throws Exception, and continue writing code.

It is a bit different with prompting and console Apps but still, don't be so afraid of the phrase

you have to think of every possible error that can occur

When you start using some framework and Java to write something close to real world app, like Web App, you'll see that usually those frameworks provide you with very easy solution, where they will automatically create Exception Pages for you, and help you resolve errors...

Thank You