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.
> 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.
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
> 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.