• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Anaonymous class intialiser exception

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i read somewhere that annymous class intialiser can throw any checked excpetion.Also declaration before read rule is not applicable to it.Then why is below code not compiling



[ October 26, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

annymous class intialiser can throw any checked excpetion.A



Where is your try-catch block??

Check the modified code below..



about y = x; // NOT OK

try this...

TopLevel t = new TopLevel() {
int x, y;
{

Cant say the reason...why it gives compile time error only for y=x and not for x=4

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cant say the reason...why it gives compile time error only for y=x and not for x=4



But, I am getting the error for x=4 also.


What could be the reason?
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lalitha, have you put x and y declarations before the initializer block ?

Concerning the Exception, your anonymous class initializer can throw any checked Exception which can be handle by your AnonymousClassTest constructors.

So your code has to be modified with something like :




[ October 26, 2005: Message edited by: Seb Mathe ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is not even an anonymous class here, apart from undefined fields, a nondeclared zero argument constructor which should throw Exception, and instance initialization blocks ending abruptly, and undeclared exceptions.

What on earth are you trying to do?
[ October 26, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code compiles when the declaration is before the block .......

int x,y;
{
x=4;
y=x; //NOT OK
try {
throw new Exception("Anonymous exception in Intializer");//NOT OK
} catch (Exception e) {
System.out.println("exception");
}
}

But it doesnt compile when the declaration is after the block
{
x=4;
y=x; //NOT OK
try {
throw new Exception("Anonymous exception in Intializer");//NOT OK
} catch (Exception e) {
System.out.println("exception");
}
}
int x,y;


My question is - When we declare the variables after the block & comment out this line : y=x; //NOT OK , it compiles. So when it can take "x" as valid, why isnt it taking "y" to be valid. Please give me a concrete reason for this. I am confused.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..



Consider the statement..

y = this.x;

Now the program compiles fine...

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the code just above:
1. There is still no anonymous class
2. There is no exception being thrown by the instance initializer which was the original intention of the code. The try and catch of the exception is not the correct solution for this problem.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry...




The try and catch of the exception is not the correct solution for this problem.



Can you please let me know...what should be the best way to approach this

situation..?


[ October 26, 2005: Message edited by: A Kumar ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Of course there is no anonymous class here..."

But is it obvious to the original poster? It is important for Patrick to understand his problem(s), rather than me solving it for him.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

what should be the best way to approach this

situation..?



What about this....?

 
patrick J. d'cousta
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi barry,
Can u please explain why instance feild t has an class created on the fly extending
TopLevelClass is not an anonymous class.
Sorry if i have caused any confusion.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It could be me who has to say sorry. Let me put some tags around that code to format it properly, I was looking lower down at the main method...


Um... yes SORRY. Now I see it...
[ October 26, 2005: Message edited by: Barry Gaunt ]
 
patrick J. d'cousta
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi members,
I just want to clear 2 fundas
1.Instance intitaliser expression of anonymous class can throw any exception bcos it doesnt have any constructor.Makes sense to me.but i dont have a running example of it.
2.Declaration before read rule for variables in intilizer block is not applicable to anonymous class.
This i am not sure...but i think should make sense cos of no constructor in anonymous class

Can anyone provide a clean example of the same?
Thanks in advance
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For 1) Yes an instance initializer in an anonymous class can throw any exceptions (JLS 3.0 section 8.6). In the same section it also says that it is a compiler error if the instance initializer cannot complete succesfully. The throw at the end of the initializer is such an abrupt ending of the instance initializer.

For 2) See JLS 3.0 section 8.3.2.3:


The declaration of a member needs to appear textually before it is used only if
the member is an instance field of a class or interface C and all of the following
conditions hold:

* The usage occurs in an instance variable initializer
of C or in an instance initializer of C.
* The usage is not on the left hand side of an assignment.
* The usage is via a simple name.
* C is the innermost class or interface enclosing the usage.

(for simplification references to static initializers removed in this quote)

For your assignment y = x, all 4 of those conditions are met. So the int x declaration must appear textually before it is used. Here C is the anonymous subclass of TopClass.

Once again I apologize for stating that there was no anonymous class, the lack of formatting had me thoroughly confused. Please use blocks in future.
[ October 26, 2005: Message edited by: Barry Gaunt ]
 
patrick J. d'cousta
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,
Thanks for the reply.One more favour can u give a clean compilin code
having the above mentoned concepts ?

Thanks in advance
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For 1):


Note that it is the main method that must handle the exception or announce that it throws the exception. You do not have to catch the exception in the anonymous class initializer.

For 2): Read the section of the JLS I mentioned above, there are code examples there.
 
The only thing that kept the leeches off of me was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic