Results 1 to 5 of 5

Thread: Exception

  1. #1
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

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

  2. #2
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default 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

  3. #3
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default 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 ?

  4. #4
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default 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 ?

  5. #5
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default 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 block
    Java Code:
    catch(Exception e){
    s.o.p.(e.getMessage());
    }
    this will display which exception is there
    Hemant Metalia
    cool

Similar Threads

  1. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  2. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  3. Replies: 1
    Last Post: 02-06-2011, 05:38 AM
  4. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •