Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts on try and finally block

 
sagar shiraguppi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
can any one please let me know what happens when we are getting expetions in try block as well as finally block and we are throwing those exception to caller method, and in caller method we have only one catch method handling Exeption class exceptions
Roughly i wrote the code here.
m1()throws ArithmaticExpression E, ArrayindexBoundOfException E
try{
some logic
..............
............
throwing arithmatic exception
...........
.....
}
finally
{
some logic
;;;;;;;
;;;;
throwing arrayindexoutofbound exception
}
}


class B
{
Public static void main(String[] args)
{
..............
.........
catch(Exception E)
{
code goes here to handle exception
}
}
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well only that exception will be caught that is in the try block...finally code will always executed no matter there is exception in the try block or not ..
if we get exception in the finally bock that will not be caught...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseCodeTags when you post code here, otherwise, it's difficult to read!

For Exception handling, try/catch/finally, overruling uncaught exceptions, have a look here, it's a wonderful resource!
 
sagar shiraguppi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@akhter
Thanks you

@Abimaran Kugathasan
Thank you that was great link
 
sagar shiraguppi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
void m1(int x,int y)throws ArithmeticException,ArrayIndexOutOfBoundsException
{
System.out.println("Begin m1");
int p=x/y;
System.out.println("Value of p is" +p);
B b = new B();
b.m2();
System.out.println("End m1");
}
};

class B
{
void m2()throws ArrayIndexOutOfBoundsException
{
System.out.println("Begin m2");
int a[] = new int[5];
a[6]=20;
System.out.println("End m2");
}
};


class Throws1
{
public static void main(String[] args)
{
System.out.println("Begin ... main");
try
{
A a = new A();
a.m1(10,2);
}
catch (ArithmeticException e)
{
System.out.println(e);
e.getMessage();
e.printStackTrace();
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
e.getMessage();
e.printStackTrace();
}
System.out.println("End ... main");
}
}


the output of this program is

Begin ... main
Begin m1
Value of p is5
java.lang.ArrayIndexOutOfBoundsException: 6
at B.m2(Throws1.java:20)
at A.m1(Throws1.java:9)
at Throws1.main(Throws1.java:34)
Begin m2
java.lang.ArrayIndexOutOfBoundsException: 6
End ... main


why begin m2 is not printing???
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
begin m2 is printing, it's right there in your output.


Hunter
 
sagar shiraguppi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@hunter

Please let me know why it is (begin m2) is printing after those exceptions??
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why your's is printing after when I run your program I receive this output:



Is this what your were expecting?

Hunter
 
sagar shiraguppi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes sir, i m expecting the output which you just got , but dont know why in my laptop is it is (begin m2) is printing after those exception
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sagar shiraguppi wrote:yes sir, i m expecting the output which you just got , but dont know why in my laptop is it is (begin m2) is printing after those exception



Keep in mind that the printStackTrace() method sends the trace to standard error. And the other print messages of the program went to standard out. So, depending on the OS, the JVM, etc., ie. the order that the two streams got flushed, you can get the output in a different order.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic