Results 1 to 6 of 6
Thread: when is throw used in try block
- 09-06-2011, 02:19 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 14
- Rep Power
- 0
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.
- 09-06-2011, 02:31 AM #2
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:
In the method1/doStudff1 version "Done" is displayed but in method2/doStuff2 it is not because the exception being thrown circumvents it happening.Java 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(); } }
- 09-06-2011, 03:05 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 14
- Rep Power
- 0
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.
- 09-06-2011, 03:17 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: when is throw used in try block
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.
- 09-06-2011, 04:03 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 14
- Rep Power
- 0
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.
- 09-06-2011, 04:07 AM #6
Similar Threads
-
Throws and Throw
By f22raptor in forum New To JavaReplies: 2Last Post: 09-04-2011, 08:28 AM -
where to use throw and throws
By javastuden in forum New To JavaReplies: 4Last Post: 11-02-2010, 03:31 PM -
About throw message
By killerf2006 in forum New To JavaReplies: 4Last Post: 08-22-2010, 11:48 AM -
what exception to throw
By DoolinDalton in forum New To JavaReplies: 5Last Post: 02-10-2010, 03:45 PM -
throw an exception
By sfe23 in forum New To JavaReplies: 3Last Post: 02-14-2009, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks