• 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

Exception problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Could someone please help me with this problem?
consider the foll code sequence
try{
//code block
}
catch(Exception1 e)
{
//code block
}
catch(Exception2 e)
{
//code block
}
catch(Exception3 e)
{
//code block
}
finally()
{
//code block
}
// Program continue
My question is, if the try was to throw an exception not defined in any of the caught blocks what would happen?
I think that the try would quit and program execution will continue.
Plz comment
-Guhan
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,
The try block will exit and flow would propagate up to the caller of this method and the a matching catch would be sought there.If it does not exist there too, the propagation would keep on through the stack trace and if no where a matching catch is still not found, the program will be terminated promptly, ungracefully.
Herbert
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose it would go through the finally block just before exiting.
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course it would !
Herbert
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All is right but keep in mind that: if you don�t put the throws statement in you method declaration and the compiler doesn�t reject your code, the exception that can be throw by te JVM is a RuntimeException.
RuntimeException inherits from Exception so, when it reaches a RuntimeException or an Exception catch, it will be caught, but you are not forced to catch this type of exception.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it ll go to finally blk.if finally blk also doesn't catch that exception program will exit.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi!
Could someone please help me with this problem?
consider the foll code sequence
try{
//code block
}
catch(Exception1 e)
{
//code block
}
finally()
{
//code block
}
// Program continue


Time to take a walk.
Walk #1
1. try block has an exception
2. exception is caught
3. finally block
4. exec continues w/in method containing try catch finally (code)
Walk #2
1. try block has an exception
2. exception is NOT caught
3. finally block NOT executed
4. method containing try catch finally (code) ends and exception
a passed to calling method
hope this helps
please correct me if i am wrong, monty6
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this code :
class except
{
public static void main(String[] args)
{
System.out.println("In main");
try
{
thro();
}
finally
{
System.out.println("Yes, I am here");
}
}
static void thro()
{
String a = new String("abc");
String b = null;
boolean c = b.equals(a); //throws NullPointerException
}
}
As I understand, finally block is always executed whatever Exception is thrown regardless of the fact whether it is caught or not!!. It is only not executed if u have a call to System.exit(0). Let me know, if there is something that I am missing.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you missed something.
The finally block won't be executed also, if there is an uncaught exception in the finally block itself.
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I said "finally block will always be executed" I only meant that control would reach finally block and then code (which is OK and not throwing exceptions. I assumed, I guess !!) in there would be executed. Now, that is quite obvious that if another exception is going to be thrown in there, it will not execute fully!!
 
Guhan Ramanan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello!
thanx a lot for all your suggestions and solutions.
Can i access this site using a news reader or ftp? If so, how do i do it? My Interent connection is rather slow and takes awhile to load.
Special Thanks to the people at JavaRanch )
-Guhan

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
finally block will be executed in both the cases ie when you catch or do not catch the exception.
In the first case, catch is executed first and then finally. then continues the program execution.
In the second case, exception is raised in the try block so checks whether is has been handled. Because it is not handled , finally block is executed and then stops the execution by throwing the error.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree finally will always be executed , but only exception is if there is no System.exit(..) stuff b4 finally blk
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic