Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Nest Try

  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default Nest Try

    Java Code:
    public class NestTry_2
    {
    	public static void main(String args[])
    	{
    		try[b]//try1[/b]
    		{
    			int a=0;
    		  System.out.println("a is= "+a);
    		  int b=42/a;
    		  
    		  try[b]//try2[/b]
    		  {
    		  	int c=0;
    		  	int d=50/c;
    		  	int f[]={1};
    		  	f[42]=99;
    		  }
    		  
    		catch(ArithmeticException e)
    		{
    			System.out.println("Div by ziro:");
    		}	
    	}	  
    		
    		catch(ArrayIndexOutOfBoundsException e)
    		{
    			System.out.println("Array is oob:");
    		}
    		
    		System.out.println("after try/catch block");
    	}
    }
    Hello
    How does the above program work?
    1)first, does try1 go on the stack?
    2)second, does try2 go on the stack after try1?
    3)How do catch clauses work?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by arefeh View Post
    How does the above program work?
    1)first, does try1 go on the stack?
    2)second, does try2 go on the stack after try1?
    3)How do catch clauses work?
    Why don't you just try (sic) it and see what your program does? If you don't understand the outcome sprinkle in some System.out.println( ... ) statements and see for yourself.

    kind regards,

    Jos

  3. #3
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I searched and got these results:
    The first program: Nested 'try' statements in Java
    Java Code:
    class Nested_Try
    {
        public static void main (String args[])
        {
            try//try1
            {
                int a = Integer.parseInt (args [0]);
                int b = Integer.parseInt (args [1]);
                int quot = 0;
    
                try//try2
                {
                    quot = a / b;
                    System.out.println(quot);
                } 
                catch (NumberFormatException e)// Exception 2
                {
                    System.out.println ("Incorrect argument type");
                }
            } 
            catch (ArithmeticException e)// Exception1
            {
                System.out.println("divide by zero");
            }
        }
    }
    1)with NumberFormatException, try1 do not have any catch.
    2)with ArithmeticException, try2 has Exception1.
    That is: inner try can use from outer catch, but outer try can not use from inner catch.OK?


    The second program: nested try catch..
    After the exception has been handled in some "catch" block, the execution
    will continue from the end of that "catch" block. If there was no matching
    "catch" block, the thread will terminate.
    …………..
    But as said, your main issue seems to be to consider try-catch a form of
    loop, which it is not. Drop that, and you'll see that the execution flow
    is natural.
    please give your idea about this subject.

  4. #4
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    We know that:

    java.lang.Object
    java.lang.Throwable
    java.lang.Exception
    java.io.IOException
    java.net.SocketException


    This program simulate above tree.
    Java Code:
    import java.io.*;
    import java.net.*;
    
    class NestedTry
    {
    	public static void main (String args[]) throws Throwable
      {
      	try 
      	{
      		try 
      		{
      			try 
      			{
      				try 
      				{
      					throw new SocketException();
              } 
              catch (SocketException se) 
              {
              	//caught here..
              	System.out.println("SocketException");
              	throw se;
              }
             } 
             catch (IOException ioe) 
             {
             	//and here..
             	System.out.println("IOException");
             	throw ioe;
             }
          } 
          catch(Exception e) 
          {
          	//and here..
          	System.out.println("Exception");
          	throw e;
          }
        } 
        catch(Throwable t) 
        {
        	//and here..
        	System.out.println("Throwable");
          throw t;
        }      
      }
    }
    output is:
    SocketException
    IOException
    Exception
    Throwable
    Exception in thread “main” java.net. SocketException
    at NestedTry.main(NestedTry.java:16)

    My question
    runnig throw e cause that this program does not complete normally.
    Why this error appear?
    Exception in thread “main” java.net. SocketException
    at NestedTry.main(NestedTry.java:16)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Um, because you're throwing the exception in the last catch (Throwable t)?

  6. #6
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I throw throw t in last catch.OK?
    The last catch can not catch this exception.OK?
    I think that stack must print this:
    Exception in thread “main” java.lang.Throwable
    at NestedTry.main(NestedTry.java:..)

    but, i do not undrestand why this error prints:
    Exception in thread “main” java.net. SocketException
    at NestedTry.main(NestedTry.java:16)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Because the Throwable is a SocketException.

  8. #8
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I am sorry!
    I did not undrestand your purpose.
    please describe more
    Last edited by arefeh; 02-01-2010 at 07:07 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    I suggest you read up on inheritance.
    A SocketException (in fact all Exceptions) are extensions of Throwable.

  10. #10
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    OK.
    I know that SocketException inherits from IOException and IOException inherits from Exception
    and Exception inherits from Throwable.

    I know that when i write throw new SocketException(), that is:
    new IOException(),new Exception(), new Throwable().

    I know that when throwing SocketException,catch (SocketException se) catches this exception.
    And catch (IOException ioe) handles throw se; and ....

    Then these exceptions handle these catches.
    But throwing throw t; in last catch is not habdled with anycatch.
    Then main's frame poped from java stack and this program does not completed normally.


    But i do not know why:
    Exception in thread “main” java.net.SocketException
    at NestedTry.main(NestedTry.java:16)


    Actually i do not know.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    And as it's grand finale the JVM prints out the exception that caused it to exit. And that exception was the SocketException.

  12. #12
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Thank you.
    But i do not undrestood yet.
    I ask not to laugh to me about this case.
    please pay attention again:
    Java Code:
    .......
    }
     } 
    catch(Throwable t) 
    { 
    //and here.. 
    System.out.println("Throwable"); 
    [b]//throw t; I picked up it from the program[/b]
       }
      } 
    }
    then new output is:
    Java Code:
    SocketException
    IOException
    Exception
    Throwable
    that is: throwing Throwable caused to produce following output:
    Exception in thread “main” java.net. SocketException
    at NestedTry.main(NestedTry.java:16)

    OK?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Yes, that's right.
    Each of the throw commands you are giving in the catch blocks is throwing the existing exception (SocketException).
    The final "throw t" is throwing it out of main(), and cvausing the JVM to exit, printing the stack trace as it does so.

  14. #14
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Now I use from printStackTrace(System.out) for all catches and I get this result:
    Java Code:
    SocketException
    java.net.SocketException
    at NestedTry.main(NestedTry.java:16)
    
    IOException
    java.net.SocketException
    at NestedTry.main(NestedTry.java:16)
    
    Exception
    java.net.SocketException
    at NestedTry.main(NestedTry.java:16)
    
    Throwable
    java.net.SocketException
    at NestedTry.main(NestedTry.java:16)
    
    Exception in thread “main” java.net.SocketException
    at NestedTry.main(NestedTry.java:16)
    Excuse me. I feel that my brain locked and I can not find the key of this lock.
    One thing is false in my thinking cap and i do not know what it is.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    It's the same, physical exception!
    You are not throwing any new exception in each of those catch blocks...you are throwing the original SocketException each time, so why do you expect the stack traces to be different?

  16. #16
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I thank you about your attention and patience

    I think that:
    1-when I throw SocketException with throw new SocketException, that is I create a new throwable object.
    2- when I write throw new SocketException, that is I create a new object with name of IoException.(= new IoException())
    3-and ….
    4-when I write
    catch (IOException ioe)
    {
    //and here..
    System.out.println("IOException");
    throw ioe;
    }

    I think that I have created a new throwable object with name of ioe not with name of se because se handled in this catch and ended. I think that now I have a new throwable object whit name of ioe and ….
    5- when I create SocketException object, I have created IoException object and Exception object and Throwable object. Then when I throw SocketException object, that is have I threw SocketException object and IoException object and Exception object and Throwable object?

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    "throw" creates nothing. It simply throws what it's told to throw. The only new instance you are creating is the SocketException. Everything else is throwing that.

    "se" is a reference to that SocketException.
    "ioe" is a reference to that SocketException.
    "e" is a reference to that SocketException.
    "t" is a reference to that SocketException.

    The only way you'd get a new exception object is if you wrote "throw new <some throwable class>".

  18. #18
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I suggest you read up on inheritance.
    Hello
    I think that you are right. Maybe i have not undrestood inheritance very well.
    please put here a link about inheritance, reference that been produced with new operator.

    thank you

  19. #19
    Drun's Avatar
    Drun is offline Member
    Join Date
    Nov 2009
    Location
    Indian Head, MD (Missing Pittsburgh PA)
    Posts
    44
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I suggest you read up on inheritance.
    A SocketException (in fact all Exceptions) are extensions of Throwable.
    This makes me wonder, does a "catch" catch only exception or any Throwable (yes I know that chances are if you are making something ot catch it would probably be best to make it an exception no mater what the anser, this is just curosity) So if I was to make a randome object and extend Throwable instead of Exception could it still be thrown and caught just llike an excpetion (mind you again I would not do this, just wondering if it is posable)
    Michael P. O'Connor
    http://www.mikeoconnor.net

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Well, yes.
    The OP is throwing a Throwable at the end of that code.

    ETA: Oh yes, to read up on inheritance then go have a look at either the Sun tutorials, or wiki.

    ETA2: In fact just go through the Sun tutorials since I would say you need to start from scratch.
    Last edited by Tolls; 02-04-2010 at 08:51 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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