Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

UnimplementedException: bad idea or not?


  • From: ehuddleston@xxxxxxxxxxxxxx (Erik Huddleston)
  • Subject: UnimplementedException: bad idea or not?
  • Date: Sat, 29 Jan 2000 19:52:01 -0600

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01BF6AC4.99CE9490
Content-Type: text/plain;
	charset="iso-8859-1"

Not implemented exceptions are a horrible design.  By implementing an
interface you are saying, "I support this contract!".  By throwing a runtime
exception for some subset of your  methods, you are breaking that contract.
I would instead refactor your design to make more granular interfaces that
you _can_ implement.  And just don't implement the parts you can't.  Your
end users can then query your component to discover the services it offers
and they can be confident that you aren't lying to them.  :-)  The JavaBeans
standard has some infrastructure for handling this in a more elegant fashion
than ClassCastExceptions.  And, of course, COM uses this pattern as a
fundamental tenet so you can check the COM literature as well.


Erik
--
Erik Huddleston, erikh@xxxxxxxx
Chief Architect, eCustomers.com
Microsoft Java MVP 

> -----Original Message-----
> From: Chris Kelly [mailto:samizdat@xxxxxxxxxxx]
> Sent: Saturday, January 29, 2000 7:18 PM
> To: advanced-java@xxxxxxxxxxxxxxxx
> Subject: UnimplementedException: bad idea or not?
> 
> 
> Is it a bad idea to throw an exception when a feature is not 
> implemented,
> but the caller of the method can't tell beforehand whether 
> that method is
> implemented?
> 
> For the sake of argument, assume you have:
> 
>   class UnimplementedException extends RuntimeException {
>     ...
>   }
> 
>   interface Interface {
>     int getA();
>     int getB();
>   }
> 
> You want to use it like this:
> 
>   Interface i = getAnObjectSomePlace();
>   System.out.println( "A=" + i.getA() + ", B=" + i.getB() );
> 
> And, you have two classes that implement Interface:
> 
>   class One {
>     int getA() { return 1; }
>     int getB() { return 1; }
>   }
> 
>   class Two {
>     int getA() { return 2; }
>     int getB() { throw new UnimplementedException(); }
>   }
> 
> Is there a better way to do this, such as by returning a numeric code
> instead of throwing an exception? You don't want to bail just because
> something is unimplemented, you just want to ignore it.
> 
> Because, the code above could become:
> 
>   Interface i = getAnObjectSomePlace();
>   int  A,B;
>   try {
>     A = i.getA();
>   }
>   catch ( UnimplementedException e ) {
>     A = -1;
>   }
> 
>   try {
>     B = i.getB();
>   }
>   catch ( UnimplementedException e ) {
>     B = -1;
>   }
> 
>   System.out.println( "A=" + A + ", B=" + B );
> 
> 
> 
> ---
> To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
> To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx
> 

------_=_NextPart_001_01BF6AC4.99CE9490
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2650.12">
<TITLE>RE: UnimplementedException: bad idea or not?</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>Not implemented exceptions are a horrible =
design.&nbsp; By implementing an interface you are saying, &quot;I =
support this contract!&quot;.&nbsp; By throwing a runtime exception for =
some subset of your&nbsp; methods, you are breaking that =
contract.&nbsp; I would instead refactor your design to make more =
granular interfaces that you _can_ implement.&nbsp; And just don't =
implement the parts you can't.&nbsp; Your end users can then query your =
component to discover the services it offers and they can be confident =
that you aren't lying to them.&nbsp; :-)&nbsp; The JavaBeans standard =
has some infrastructure for handling this in a more elegant fashion =
than ClassCastExceptions.&nbsp; And, of course, COM uses this pattern =
as a fundamental tenet so you can check the COM literature as =
well.</FONT></P>
<BR>

<P><FONT SIZE=3D2>Erik</FONT>
<BR><FONT SIZE=3D2>--</FONT>
<BR><FONT SIZE=3D2>Erik Huddleston, erikh@xxxxxxxx</FONT>
<BR><FONT SIZE=3D2>Chief Architect, eCustomers.com</FONT>
<BR><FONT SIZE=3D2>Microsoft Java MVP </FONT>
</P>

<P><FONT SIZE=3D2>&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=3D2>&gt; From: Chris Kelly [<A =
HREF=3D"mailto:samizdat@xxxxxxxxxxx";>mailto:samizdat@xxxxxxxxxxx</A>]</F=
ONT>
<BR><FONT SIZE=3D2>&gt; Sent: Saturday, January 29, 2000 7:18 PM</FONT>
<BR><FONT SIZE=3D2>&gt; To: advanced-java@xxxxxxxxxxxxxxxx</FONT>
<BR><FONT SIZE=3D2>&gt; Subject: UnimplementedException: bad idea or =
not?</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Is it a bad idea to throw an exception when a =
feature is not </FONT>
<BR><FONT SIZE=3D2>&gt; implemented,</FONT>
<BR><FONT SIZE=3D2>&gt; but the caller of the method can't tell =
beforehand whether </FONT>
<BR><FONT SIZE=3D2>&gt; that method is</FONT>
<BR><FONT SIZE=3D2>&gt; implemented?</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; For the sake of argument, assume you =
have:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; class UnimplementedException =
extends RuntimeException {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; ...</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; interface Interface {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; int getA();</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; int getB();</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; You want to use it like this:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; Interface i =3D =
getAnObjectSomePlace();</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; System.out.println( =
&quot;A=3D&quot; + i.getA() + &quot;, B=3D&quot; + i.getB() );</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; And, you have two classes that implement =
Interface:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; class One {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; int getA() { return 1; =
}</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; int getB() { return 1; =
}</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; class Two {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; int getA() { return 2; =
}</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; int getB() { throw new =
UnimplementedException(); }</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Is there a better way to do this, such as by =
returning a numeric code</FONT>
<BR><FONT SIZE=3D2>&gt; instead of throwing an exception? You don't =
want to bail just because</FONT>
<BR><FONT SIZE=3D2>&gt; something is unimplemented, you just want to =
ignore it.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Because, the code above could become:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; Interface i =3D =
getAnObjectSomePlace();</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; int&nbsp; A,B;</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; try {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; A =3D i.getA();</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; catch ( UnimplementedException e ) =
{</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; A =3D -1;</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; try {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; B =3D i.getB();</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; catch ( UnimplementedException e ) =
{</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; B =3D -1;</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp; System.out.println( =
&quot;A=3D&quot; + A + &quot;, B=3D&quot; + B );</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; ---</FONT>
<BR><FONT SIZE=3D2>&gt; To unsubscribe, mail =
advanced-java-unsubscribe@xxxxxxxxxxxxxxxx</FONT>
<BR><FONT SIZE=3D2>&gt; To get help, mail =
advanced-java-help@xxxxxxxxxxxxxxxx</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01BF6AC4.99CE9490--

---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx