• 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

Initializer block

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ref. from khalids book(P.no-261)


Initializer Expression can not pass on checked exceptions, only unchecked ones.If any checked exception is thrown during execution of initializer expression , I must be caught and handled within initializer expression.


What does this means ? I tried to throw a both unchecked and checked exception and it worked. I dont understand the meaning of can not pass on checked exception. only unchecked ones
Rashmi
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashmi,
I just tried out the below code, which is throwing
a checked exception in the initializer block.
It does not compile. As instance initializer blocks
cannot be calling explicitly we have catch the checked
exception within the block itself.
So once i put a try-catch block the code compiles fine.
class Q28fd {
{
int i = 10;
throw new MyException(); // need to catch the exception
}
public static void main(String args[]) {
}
}
Correct me if wrong .
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
I posted this Q. and after some time i gor its answer!
Actually if the code would have been---
class Q28fd {
{
int i = 10;
throw new RuntimeException(); // no need to catch the unchecked exception
}
public static void main(String args[]) {
}
}
It compiles as an unchecked exception is thrown and not caught. It is allowed. But not for checked one. This is what that stmt means.
Thanx a lot for replying!
Rashmi
 
BWA HA HA HA HA HA HA! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic