Results 1 to 20 of 21
Thread: Nest Try
- 01-31-2010, 11:13 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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"); } }
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?
- 01-31-2010, 03:28 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 01-31-2010, 04:34 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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"); } } }
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.
- 02-01-2010, 03:13 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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; } } }
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)
- 02-01-2010, 04:28 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Um, because you're throwing the exception in the last catch (Throwable t)?
- 02-01-2010, 05:57 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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)
- 02-01-2010, 05:59 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Because the Throwable is a SocketException.
- 02-01-2010, 08:05 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
I am sorry!
I did not undrestand your purpose.
please describe moreLast edited by arefeh; 02-01-2010 at 08:07 PM.
- 02-02-2010, 12:04 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
I suggest you read up on inheritance.
A SocketException (in fact all Exceptions) are extensions of Throwable.
- 02-02-2010, 05:01 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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.
- 02-02-2010, 05:25 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
And as it's grand finale the JVM prints out the exception that caused it to exit. And that exception was the SocketException.
- 02-02-2010, 06:12 PM #12
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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] } } }
Java Code:SocketException IOException Exception Throwable
Exception in thread “main” java.net. SocketException
at NestedTry.main(NestedTry.java:16)
OK?
- 02-02-2010, 06:15 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.
- 02-02-2010, 10:44 PM #14
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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)
One thing is false in my thinking cap and i do not know what it is.
- 02-03-2010, 09:54 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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?
- 02-03-2010, 11:57 AM #16
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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?
- 02-03-2010, 12:22 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
"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>".
- 02-03-2010, 08:07 PM #18
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
- 02-03-2010, 10:16 PM #19
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
- 02-04-2010, 09:48 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 09:51 AM.
Bookmarks