• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubts on try and finally block

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@akhter
Thanks you

@Abimaran Kugathasan
Thank you that was great link
 
sagar shiraguppi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
begin m2 is printing, it's right there in your output.


Hunter
 
sagar shiraguppi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic