• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

exceptions - possible mistake in OCA practice book page 312

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see there are two possible mistakes in the second example on page 312 because when I compile and run this code I see a different behavior.

public static void main(String[] args){
try{
    throw new RuntimeException();
        } catch(RuntimeException e){
System.out.println("Inside catch block from main try- before throwing a RuntimeException");
throw new RuntimeException("Throwing a RuntimeException from catch block");
        } finally{
throw new Exception();
}
}

-> I added text to make it easier to follow.


First, if I try to compile the code as it is, I get this error:
error: unreported exception Exception; must be caught or declared to be thrown
throw new Exception();
Which is right because the program require to handle or declare checked exceptions.

So, to continue with the example making some modifications, I choose handle the Exception like this:

try{
  throw new RuntimeException();
    } catch(RuntimeException e){
System.out.println("Inside catch block from main try- before throwing a RuntimeException");
throw new RuntimeException("Throwing a RuntimeException from catch block");
    } finally{
try{
throw new Exception();
} catch(Exception e){
System.out.println("Inside catch from finally block");
}
}
}

When I compile it, the order is the follow:

Inside catch block from main try- before throwing a RuntimeException
Inside catch from finally block
Exception in thread "main" java.lang.RuntimeException: Throwing a RuntimeException from catch block
at Test.main(Test.java:7)

So, in my opinion, the book fails when it says: Since finally block throws an exception of its own on line 31, this one gets thrown. The exception from the catch block gets forgotten about.

Because finally, the exception from catch block is thrown like we can see here: Exception in thread "main" java.lang.RuntimeException: Throwing a RuntimeException from catch block
at Test.main(Test.java:7)

What I see is that the exception from catch block is thown after the exception thrown by finally block, but this is not forbidden.
 
Rancher
Posts: 4190
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you handle the Exception thrown in the finally block inside that block, so it does not override the RuntimeException thrown inside the catch block.

Try this instead:

 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pablo Napoli wrote:I see there are two possible mistakes in the second example on page 312 because when I compile and run this code I see a different behavior.

public static void main(String[] args){
try{
    throw new RuntimeException();
        } catch(RuntimeException e){
System.out.println("Inside catch block from main try- before throwing a RuntimeException");
throw new RuntimeException("Throwing a RuntimeException from catch block");
        } finally{
throw new Exception();
}
}

-> I added text to make it easier to follow.hello...

First of all how the exceptions are propogated we need know, first catch block catches the exception and again there is rethrow exception is there but that exception is not handled so it may throw exception if didn't provide finally block, so logic is finally block overrule all the priviesly uncaught exception and print the only exception which is in finally block, it is checked exception so compile time error, if it is runtime error then it will compile well but throw exception at runtime,

First, if I try to compile the code as it is, I get this error:
error: unreported exception Exception; must be caught or declared to be thrown
throw new Exception();
Which is right because the program require to handle or declare checked exceptions.

So, to continue with the example making some modifications, I choose handle the Exception like this:

try{
  throw new RuntimeException();
    } catch(RuntimeException e){
System.out.println("Inside catch block from main try- before throwing a RuntimeException");
throw new RuntimeException("Throwing a RuntimeException from catch block");
    } finally{
try{
throw new Exception();
} catch(Exception e){
System.out.println("Inside catch from finally block");
}
}
}

When I compile it, the order is the follow:

Inside catch block from main try- before throwing a RuntimeException
Inside catch from finally block
Exception in thread "main" java.lang.RuntimeException: Throwing a RuntimeException from catch block
at Test.main(Test.java:7)

So, in my opinion, the book fails when it says: Since finally block throws an exception of its own on line 31, this one gets thrown. The exception from the catch block gets forgotten about.

Because finally, the exception from catch block is thrown like we can see here: Exception in thread "main" java.lang.RuntimeException: Throwing a RuntimeException from catch block
at Test.main(Test.java:7)

What I see is that the exception from catch block is thown after the exception thrown by finally block, but this is not forbidden.

in your second code your are written the try catch in finally block which is legal and thrown a checked exception so catch block handeld it so normal execution continues, means before terminating the method finally block exicuited and there is uncaught runtime exception is there so at runtime it displays the message of exception which is not handled in the catch block means rethrow exception in catch block
 
Pablo Napoli
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, thanks guys for your answerrs. But Dave, specially about you told me (that it clarify me the code), so I suppose the code that I showed from the book is wrong because it's throwing a checked exception while it's not been handled.
 
Marshal
Posts: 65110
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pablo Napoli wrote:. . . in my opinion, the book fails when it says: Since finally block throws an exception of its own on line 31, this one gets thrown.  . . .

No, the book would appear to be correct. You are throwing an exception in the try, handling it in thecatch, and replacing the exception from the catch by that thrown in the finally.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!