try , catch and throw

Hiwhen do we use "try & catch" and throws ?( I mean when try &catch is preferred over throws or the reverse ?)
[139 byte] By [Sangfroida] at [2007-11-27 7:44:51]
# 1
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
Hippolytea at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 2

Eh? They're opposites.

When you're tossing a ball back and forth, when do you throw and when do you catch? Answer: at the appropriate time.

You try/catch when you want to try invoking some methods that might throw an exception. You throw when your current code has found that something is wrong, and needs to halt execution of that current method.

paulcwa at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 3

> Eh? They're opposites.

>

> When you're tossing a ball back and forth, when do

> you throw and when do you catch? Answer: at the

> appropriate time.

>

> You try/catch when you want to try invoking some

> methods that might throw an exception. You throw

> when your current code has found that something is

> wrong, and needs to halt execution of that current

> method.

I think the question is more of a when is it propery to catch an exception and when is it proper to throw that same exception. From many others who have posted on this topic the consensus seems to be throw it until it can be dealt with properly. If level a can't deal with it throw it to level b if level b can't deal with it throw it to level c. Basically throw it up until the code would be in a position to make a decision with the information presented.

Aknibbsa at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 4
So the choice is: catch and handle the exception or let it propagate. Writing catch it or throw it is misleading.
Hippolytea at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 5

I think it's also helpful to be clear about the situation:

1) there's the decision to throw a new exception, at a point when no exceptions are being caught

2) there's the decision what to do when an exception, which has already been thrown (or will have been thrown, depending on your point of view) farther down the call stack (or further up the call stack, depending on your point of view, god this can be hard to describe).

2.1) you can basically ignore the exception, and let it propagate up the call stack

2.2) you can catch the exception, deal with it, and not have flow of control go any further up the stack, or

2.3) you can catch the exception, and then:

2.3.1) re-throw it

2.3.2) throw a new exception, possibly wrapping the existing one

paulcwa at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 6
i had gone through http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html before posting this topic....still i am confused when to exactly use which one..
Sangfroida at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 7

> i had gone through

> http://java.sun.com/docs/books/tutorial/essential/exce

> ptions/index.html

>

> before posting this topic....

>

> still i am confused when to exactly use which one..

How can anybody help you? We don't know what you're having problems with. Unless you give more specific information, the best we can do is repeat what's in the tutorial, which would be pointless for a couple of reasons.

jverda at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 8
i mean.... when do we use "try & catch" and throws ?
Sangfroida at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...
# 9
To make code good.More precisely, to make code more robust.There's little more than can be said given that question.
paulcwa at 2007-7-12 19:25:28 > top of Java-index,Java Essentials,New To Java...