• 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

Exceptions

 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 2 questions.
If an uncaught exception occurs, what happens to the program. I read that, it searches for another try block which catches the thrown error.
In a try-catch block if the catch block also throws an exception, what happens to the program?.
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ransika desilva:
I have 2 questions.
If an uncaught exception occurs, what happens to the program. I read that, it searches for another try block which catches the thrown error.


The best way to find out what happens is to write a program that does this.
If an uncaught exception occurs & if the calling method's signature does not have a throws statement or does not throw the uncaught exception or any of its super then the program will not process anything further; if it is an application, it will terminate.

Originally posted by ransika desilva:
In a try-catch block if the catch block also throws an exception, what happens to the program?.


Depends. If the calling method has a catch block for this exception or its super then the control will flow there. If the calling method, instead, has 'throws' statement for this this exception or its super then the control will flow to the calling method's caller & thus up the line till it either finds a matching catch block or terminates if a matching catch block is not found.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you throw something in the try block, it can be caught in the associated catch block - if you catch something compatible with the thrown exception. If you throw something in the catch block, the exception will bubble up to the next appropriate catch block, either in the same method or not. Say MyException A & B both extend Exception:

The MyExceptionA is not caught, and will start seeking a catch that does match.

This time we catch one exception and throw another, kinda like converting the exception from one type to another, but we also lost the original stack trace.
You might want to download Thinking In Java. As I recall Bruce Eckel has a pretty good chapter on exceptions. Hope that helps!
 
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic