when is throw used in try block
In Exception Handling,
I read that "throw" is used to throw the exception object in try block for user defined exceptions.
As far as I know, if the exception occurs in try block, it would be handled by catch block.
For Ex:
class Test{
public static void main(String args[]){
try{
System.out.println(args[0]/args[1]);
}
catch(Exception e){
System.out.println("Error Occured");
}
}
}
In the above pgrm, throw clause is not used, so is it that throw is only used for user defined exceptions.
Can someone please explain..
Thanks.
Re: when is throw used in try block
It depends upon the purpose of the method. If there was some code in the method that you always wanted to run then you could include it in a finally clause regardless if an excpetion was thrown or not. Consider the following example:
Code:
public class Test {
public void run() {
method1();
method2();
}
private void method1() {
try {
doStuff1(-1);
} catch(Exception e) {
e.printStackTrace();
}
}
private void doStuff1(int value) throws Exception {
try {
if(value < 0) {
throw new Exception("Danger Will Robinson!");
}
} catch(Exception e) {
throw e;
} finally {
System.out.println("Done!");
}
}
private void method2() {
try {
doStuff2(-1);
} catch(Exception e) {
e.printStackTrace();
}
}
private void doStuff2(int value) throws Exception {
if(value < 0) {
throw new Exception("Danger Will Robinson!");
}
System.out.println("Done!");
}
public static void main(String[] args) throws Exception {
new Test().run();
}
}
In the method1/doStudff1 version "Done" is displayed but in method2/doStuff2 it is not because the exception being thrown circumvents it happening.
Re: when is throw used in try block
So its not necessary that 'throw' should be used only for user defined exceptions, it can be used anywhere depending on the situations or conditions of the program. 'throw' can be used within try block, but its not compulsory. and throw can be used in a method that throws an Exception (a method declared with throws clause) but not compulsory (it can be handled using try catch block without throw).
Please correct me if Iam wrong.
Thanks.
Re: when is throw used in try block
Quote:
Originally Posted by
cooladithi4u@yahoo.com
So its not necessary that 'throw' should be used only for user defined exceptions
That's right: you can throw an exception of any kind once you recognise that an exception has happened but you don't want to deal with it (or can't deal with it). You throw the exception and let the caller deal with it.
As Junky's example shows you can throw the exception but also have a "finally" section that will do any required clean up or other work.
Re: when is throw used in try block
Only thing Im unsure with the example is that:
why are we declaring main method with 'throws Exception', why not the run() because run() is calling the methods, method1 and method2 which actually may throw exceptions.
Re: when is throw used in try block
Quote:
Originally Posted by
cooladithi4u@yahoo.com
Only thing Im unsure with the example is that:
why are we declaring main method with 'throws Exception',
That was left over from a different example. Just ignore it.