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
.



Relevant Pages

  • Re: finalize()
    ... abstract class DoSomething { ... public void doSomething() throws Exception { ...
    (comp.lang.java.programmer)
  • Re: Handling exceptions in functions
    ... returns false and raise the exception, ... procedure DoSomething(aParam: Integer); ... DoSomething(aParam2); ... So, for example, expected errors in a program may be ...
    (borland.public.delphi.non-technical)
  • Re: catch (...)
    ... "Tom Groszko" wrote in message ... > {dosomething(); ... > {report some error ... nice enough to base his exception class on std::exception you should be able ...
    (comp.lang.cpp)
  • Re: Is it possible for a try...finally...end construct to fail?
    ... :) try/finally and try/except are completely different ... side effect of Delphi's global exception handler). ... Probably show an error message to the user at this ...
    (borland.public.delphi.language.objectpascal)
  • Re: preserve exception within try .. finally
    ... The exception raised by doSomething() travels all the way up the call stack ... getResource() # don't free if it cannot be acquired ... I think that the canHandleIttest and the reraising is not very common, ...
    (comp.lang.python)