David V�squez Estrada wrote:
> [...]
> Well, the problem is that I need to override a method of the class MyClass, creating
> an anonymous class, if the constructor of this class does not throws an exception, I
> can do the following with out problems:
>
> oneclass=new MyClass("Test", 1)
> {
> public int myMethod(int i) {return i++; }
> }
>
> But, if the constructor throws an exception the code above is not correct for the
> compiler, even if I make the following:
>
> try{
> oneclass=new MyClass("Test", 1)
> {
> public int myMethod(int i) {return i++; }
> }
> }
> catch(Exception e){
> //deal with the exception here..
> }
>
> I don't why this is not possible, and there is no documentation about that.
>
> David
> [...]
Hi :-) I can do it with "J2SE1.3 + winnt40", do you use another kind of J2SE?
the following is testing code, as a reference.
BTW, if the constructor only "throw" "unchecked exceptions" such as Error or
RuntimeException, you don't need to:
- "throws" them in constructor
- "try-cacth" them when you make a instance of that class(so constructor is invoked)
public class override_nonnameClass{
public static void main (String args[]){
//try{
new AnotherClass(0){
public void myMethod(){
System.out.println("after overrided...");
}
}.myMethod();
//}catch(/*Throwable*/ /*Exception*/ ClassNotFoundException
te){te.printStackTrace();}
}
}
class AnotherClass{
private int i;
public AnotherClass(int i) /*throws*/ /*Throwable*/ /*Exception*/
/*ClassNotFoundException*/{
this.i=i;
if(i==0) {throw new /*Throwable()*/ Error() /*Exception()*/ /*RuntimeException()*/
/*ClassNotFoundException()*/ ;}
}
public void myMethod(){
System.out.println("before overrided...");
}
}
Bo
June 07, 2001
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html