|
|
Unreported Exception error message in an anonymous class |
Summary: When JBuilder compiles an anonymous class, it is required to provide
default anonymous constructors. However, some anonymous classes can throw
exceptions in the constructors and so these must be declared by the compiler.
JBuilder fails to provide the throws clause, thus causing the error message.
Because one isn't allowed to define an explicit constructor for an anonymous
class, the workaround is to convert the anonymous class into a named inner
class and explicitly declare the constructor and its throws clause there.
Example code from Vito:
Here follows a more straightforward TestClass that reproduces the bug,
followed by a TestClass2 that compiles correctly with JBuilder4:
----------TestClass
compilin with JBuilder 4 gives error "TestClass.java": Error #: 360 :
unreported exception: java.io.FileNotFoundException; must be caught or
declared to be thrown at line 15, column 43,
while compiles correctly with javac:
package test;
import java.io.*;
public class TestClass {
public TestClass() {
try {
instantiateAndClose();
}
catch (Exception x) {
}
}
public void instantiateAndClose() throws Exception{
FileInputStream fi=new FileInputStream("testfile"){
protected void finalize() throws IOException {
}
};
fi.close();
}
}
-----------------------------
Follows TestClass2, where I declare the class MyFileInputStream with the
correct constructor, that compiles correctly with JBuilder4:
---------TestClass2
package test;
import java.io.*;
public class TestClass2 {
class MyFileInputStream extends FileInputStream {
public MyFileInputStream(String name) throws FileNotFoundException {
super(name);
}
protected void finalize() throws IOException {
}
}
public TestClass2() {
try {
instantiateAndClose();
}
catch (Exception x) {
}
}
public void instantiateAndClose() throws Exception{
FileInputStream fi=new MyFileInputStream("testfile");
fi.close();
}
}
---------------
Vito.
2001-Feb-12 9:59pm gyles19@visi.com | |
| [Append to This Answer] | |
| 2001-Feb-12 9:59pm |
| Previous: |
|
| Next: |
|
| ||||||||
|
Java Community
Java Tools |
Code Central JB OpenTools |
Community Recent Threads |
Borland Chat Chat FAQ |
||||||||||||||
|
Feature Matrices 3.0 | 3.5 | 4.0 5.0 | 6.0 | 7.0 |
Shop for JBuilder |
JBuilder Downloads and reg keys |
Report Piracy | ||||||||||||||
|
Online Manuals 4.0 | 5.0 | 6.0 7.0 |
Borland DevSupport's Bug/RFE Form Quality Central Client |
JB Patches & Updates |
JBuilder FAQs TIs | ||||||||||||||
|
JBuilder Newsgroups |
Tamaracka's News Archive |
Mr. Haki's JBuilder Machine |
JGuru's JBuilder FAQ |
||||||||||||||
|
Sun's Bug Parade |
|||||||||||||||||
|
|||||||||||||||||