This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception problem" Watch "Exception problem" New topic
Author

Exception problem

Guhan Ramanan
Greenhorn

Joined: Jun 29, 2000
Posts: 7
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

<B> Guhan Ramanan </B><BR>Sun certified Programmer for Java2 Platform
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
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
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
I suppose it would go through the finally block just before exiting.
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Of course it would !
Herbert
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
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.
vaibhav deshmukh
Greenhorn

Joined: Jun 28, 2000
Posts: 7
it ll go to finally blk.if finally blk also doesn't catch that exception program will exit.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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

Joined: Jun 13, 2000
Posts: 66
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 13, 2000
Posts: 66
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

Joined: Jun 29, 2000
Posts: 7
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

Joined: Nov 22, 2008
Posts: 18944

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

Joined: Jul 06, 2000
Posts: 19
i agree finally will always be executed , but only exception is if there is no System.exit(..) stuff b4 finally blk
 
 
subject: Exception problem