Two Laptop Bag*
The moose likes Beginning Java and the fly likes doubts on try and finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubts on try and finally block" Watch "doubts on try and finally block" New topic
Author

doubts on try and finally block

sagar shiraguppi
Ranch Hand

Joined: Jul 16, 2007
Posts: 74
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

Joined: Mar 02, 2009
Posts: 151

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


Start Earning Online||Start Earning Using Java
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
sagar shiraguppi
Ranch Hand

Joined: Jul 16, 2007
Posts: 74
@akhter
Thanks you

@Abimaran Kugathasan
Thank you that was great link
sagar shiraguppi
Ranch Hand

Joined: Jul 16, 2007
Posts: 74
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

Joined: Mar 13, 2009
Posts: 492

begin m2 is printing, it's right there in your output.


Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
sagar shiraguppi
Ranch Hand

Joined: Jul 16, 2007
Posts: 74
@hunter

Please let me know why it is (begin m2) is printing after those exceptions??
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Jul 16, 2007
Posts: 74
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
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubts on try and finally block