Results 1 to 5 of 5
Thread: Exception
- 10-06-2011, 02:14 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Exception
I am now reading Exception handling. I am reading it again and again. But i am unable to understand the concept fully. Here I am going illustrate my doubt. Anybody plz help me...
1) How the "throw" and "throws" clause works step by step. Can we use all the clauses of exception handling in one go.
2) When we are using "try-catch" clause, will the statement in the try block execute, even we keep the "try" clause within a "for" loop. Look at the following code:-
import java.util.Random;
class MyExc{
public static void main(String args[]){
int a = 0, b=0, c=0;
Random r = new Random();
for(int i=0; i<3200; i++)
try{
b= r.nextInt();
c= r.nextInt();
a= 12345/(b/c);
} catch(ArithmeticExcxeption e){
S.o.p("Division B...");
a = 0;
} S.o.p("a: " + a);
}
}
}
Now i want to know will the stmt within "try" block never execute ? Or it will be executing till the b and c becomes zero.
3) If the statement within "try" block does not execute, the why we are using Exception Handling mechanism. Because there may be some important statement, within that "try" block, which must be executed.
- 10-06-2011, 02:58 PM #2
Member
- Join Date
- Feb 2011
- Location
- Ahmedabad
- Posts
- 36
- Rep Power
- 0
Re: Exception
Hi shahnawazzzzzz
the meaning of throw and throws is as follow
1)
throws
Describes the exceptions which can be raised by a method.
throw
Raises an exception .
2)
try block do not stop executing the statements but when any error found it stops execution from there and transfer the control to the corresponding catch block.
you can also go through following link it may assistwww.dlhoffman.com/classnotes/csci360-s03/csci360-18.ppt you.Hemant Metalia
cool-.gif)
- 10-06-2011, 03:31 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Re: Exception
[QUOTE=hemant_metalia;238398]
2)
try block do not stop executing the statements but when any error found it stops execution from there and transfer the control to the corresponding catch block.
You have told the stmt. My doubt is:-
will it print any stmt before the execution stops. Or it will terminate the entire "try" block, when the execution stops ? If the entire "try" block is going to be terminated(that means, it is unneccesery), then why we are going to use that part and making it so complex ?
- 10-06-2011, 03:32 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Re: Exception
[QUOTE=hemant_metalia;238398]
2)
try block do not stop executing the statements but when any error found it stops execution from there and transfer the control to the corresponding catch block.
You have told the stmt. My doubt is:-
will it print any stmt before the execution stops. Or it will terminate the entire "try" block, when the execution stops ? If the entire "try" block is going to be terminated(that means, it is unneccesery), then why we are going to use that part and making it so complex ?
- 10-06-2011, 03:49 PM #5
Member
- Join Date
- Feb 2011
- Location
- Ahmedabad
- Posts
- 36
- Rep Power
- 0
Re: Exception
If the program any exception is thrown it finds corresponding catch block if the arithmatic exception is there it will print Division B..
Rather if you wann to check more you add another catch blockthis will display which exception is thereJava Code:catch(Exception e){ s.o.p.(e.getMessage()); }Hemant Metalia
cool-.gif)
Similar Threads
-
unreported exception java.lang.Exception
By Marisabel in forum New To JavaReplies: 6Last Post: 05-01-2011, 03:21 AM -
unreported exception java.lang.Exception; must be caught or declared to be thrown
By arc_remiel in forum New To JavaReplies: 5Last Post: 02-14-2011, 11:39 PM -
How to throw exception w/RMI from Server->Client w/o "nested exception..." mentioned
By nicoeschpiko in forum Advanced JavaReplies: 1Last Post: 02-06-2011, 05:38 AM -
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks