John Wilson | 8 Feb 08:21

Re: Improvement in handling exception


On 8 Feb 2007, at 06:11, Daniel.Sun wrote:

> Hi all,
>
> "the Groovy methods will not declare that they throw any checked  
> exceptions
> unless you’ve explicitly added the
> declaration, even though they might throw checked exceptions at  
> runtime."
> quoted from 'Groovy in Action'
>
> I think Groovy should compel programmers to add 'throws XXXException'
> explicitly as Java do when defining a method that can throw some  
> exception.
>
> Why do I think it very vital?
> For example,
> If I define an exception of my own.
>
> class MyException extends Exception {
>   MyException(String msg) {
>     super(msg)
>   }
> }
>
> then I instantiate MyException and throw it in myMethod1
>
> def myMethod1() {
>   throw new MyException("error root")
> }
>
> then I invoke myMethod1 in myMethod2
>
> def myMethod2() {
>   //... omit
>   myMethod1()
>   //...omit
> }
>
> //...invoke myMethodx() in myMethody()
>
> At last I invoke myMethody in myMethodn which is vital for our  
> application
> and should be robust and shouldn't crash.
> but I can NOT remember that myMethody can throw MyException. What  
> is worse,
> I have NOT enclosed "myMethody()" in try-catch clause. Disaster  
> appeared :(
> myMethodn will crash in some day.
>
> def myMethodn() {
>    //... omit
>   myMethody()
>   //...omit
> }

The debate about checked vs unchecked exceptions is very old. Java  
has checked exceptions (put into the language at the very last  
minute), C# does not. There have been many discussions about the  
robustness of C# applications vs Java ones due to this difference. In  
practice there seems not to be any difference.

We took a decision right at the start of the project not to require  
users to do housekeeping of checked exceptions and this seems to work  
very well.

I do, however, think that  we could improve our Exception handling  
whit respect to Java interfacing. If a groovy method throws a checked  
exception but does not have a throws clause then a Java method which  
calls it cannot catch the exception. The Groovy compiler can tell  
that a checked exception is thrown and could, in principle, put the  
throws clause into the generated bytecode.

John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Gmane