• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

not understanding the try catch behaviour

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source : examlab..

here i am not getting the behaviour of try catch in outer try catch ....

can anyone please make me clear about it ?
trycatch.jpg
[Thumbnail for trycatch.jpg]
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in inner catch block you are re-throwing the same exception, then control will go to the inner finally[since finally block is always executable regardless of exception], then outer catch, finally respectively.
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shouldn't the order must be the same ??? i mean try finally and after that catch in inner try catch then it should also be the same in outer also..

I am not understanding why the sequence differs in outer try catch...`
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is the same as you are saying. When you run the attached program you get the result as mentioned below -

Begin
Inside Try
Inside finally 1
Inside throwable
Inside finally 2


And the sequence of activities happens in the given order and the control would

1- Go to out try "Printing Begin"
2- Go to inner try "Printing Inside Try"
3- Go to inner catch, but as we are throwing the exception before executing system.out.println statement, we do not get "Inside Runtime" in the console
4- Go to finally block "Printing Inside finally 1"
5- Go to throwable catching block as the exception thrown from inside is not ClassCastException and print "Inside throwable"
6- Go to outer finally bock printing "Inside finally 2"

Please let me know which two statements you do not feel to be in correct sequence
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i think the output should be is

begin
inside try
inside finally-1
inside runtime
inside finally-2
inside throwable


what i think of the order in try catch is

try -- > finally --> catch (if any exception occurs)

can you please clarify this ???
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me the order seems fine, I think the reason you don't get what what you get is the the inner catch is only catching RuntimeException subclasses, whereas the other catches Throwable. What type of Exception are you throwing from your throwIt() method?

Sean
 
Get off me! Here, read this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic