• 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

Static/Instance Block Exceptions Handling

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I Have Read that We have to catch the Checked Exception in static block. But in Initiallization Block This can Also be handeled by writing throws clause in the constructor. The Concept is clear to me but can any body tell why this code is not working
1) class A {
A() throws InterruptedException {}
{ // block starts here

throw new InterruptedException();
}
}
Please Let me Know If a m wrong somewhere
Thanks
Naveen
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Naveen, could you give a more detailed code and explain what's your expetation? I'm at a loss as for where your problem lies?
 
Naveen Sharma
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
Is it true that in an Initiallising Block,
If some of your code throws any kind of checked exception without applying try and catch can be handeled by applying "throws" clause in the constructor
If Yes Then How
for example
class A
{
A()throws InterruptedException{} //constructor

{ /// initialising block
throw new InterruptedException
}
}
Is The Above Code Ok
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Naveen,
from where have u read that exceptions can be thrown in initialiser blocks.
when u compliled this code u might have got errors saying that iniatialiser
blocks whether static or instance can't throw exceptions.this is the right concept.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naveen,
According to the JLS you should be able to use a throws clause in an instance initializer and have the ctors handle it but the compilers won't allow it.
There is an official 'bug' report for the behaviour you are seeing. See this earlier discussion on the same topic
http://www.javaranch.com/ubb/Forum24/HTML/007863.html
Hope that helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic