Re: finalize()
cy wrote:
class File {
void close(){}
}
abstract class DoSomething {
public abstract void doIt();
public void doSomething() throws Exception {
File file = null;
try {
this.doIt();
} finally {
if(file != null) {file.close();}
}
}
}
public class Doer extends DoSomething {
public void doIt() {};
You failed to implement the abstract method.
public void doSomething() throws Exception {
throw new Exception("i hope this doesn't cause any problems");
}
The idea is not to override doSomething() but to let the superclass
doSomething() call the overridden doIt().
public static void main(String[] args) {
Doer d = new Doer();
d.doSomething();
This must be line 26. (You might have considered helping on this matter.)
You failed to catch the Exception that doSomething() might throw.
System.gc();
This is a dilatory call. It is not guaranteed to run the gc, and if it does
might cause a full generational collection instead of a simple nursery one,
thus possibly reducing performance.
}
}
----------------
error: line 26: unreported exception java.lang.Exception; must be
caught of declared to be thrown: d.soSomething():
The call to d.doSomething() (notice where the message points that out?) might
throw an Exception, which "must be caught *or* declared" [emphasis added - you
misspelled the message - next time copy and paste it to avoid typos], just
like the message says. Either put a try ... catch around the call or declare
the method that uses it to rethrow the Exception. (Which makes no sense to do
with main().)
---------------
make sense?
-----------
Perfect sense.
- Lew