Re: Example of misusage of exceptions.

From: Skybuck Flying (nospam_at_hotmail.com)
Date: 11/01/04


Date: Mon, 1 Nov 2004 19:36:26 +0100


"Nicolai Hansen" <nic@aub.dk> wrote in message
news:d96764ff.0411010304.36146331@posting.google.com...
> "Skybuck Flying" <nospam@hotmail.com> wrote in message
news:<cm38jr$6mu$1@news6.zwoll1.ov.home.nl>...
> > Hello,
> >
> > First I like to say I appreciate that this person has made a website
about
> > adaptive huffman compression and even supplied some code.
> >
> > However the code is pretty shocking ;) and a good example of misusage of
> > exceptions... probably because out of lazyness ;)
> >
> > http://www.cs.sfu.ca/cs/CC/365/li/squeeze/AdaptiveHuff.html
> >
> > This person/code is using/throwing exceptions instead of returning a
boolean
> > value as I think that he should have done in the first place.
> >
> > The first function tries to find a character if it can't find it it
simply
> > (?!) throws an exception.
> >
> > The second function tries to find a tree node with a certain frequency
if it
> > doesn't find it it also throws an exception.
> >
> > The update tree method has a try catch block... this whole construction
> > obfuscates the logic path quite considerably...
> >
> > For example at first glance it looks like the 'catch' block can be
'entered'
> > from both functions... in reality however the second function will
always
> > find the frequency... so the programmer did not clearly express that in
the
> > code.
> >
> >
> > // two functions from other class.
> >
> > /**
> > * findChar
> > *
> > * Returns the index of the character
> > *
> > */
> >
> > public int findChar (char letter) throws NoSuchElementException
> > {
> >
> > for (int i=0; i < 2*ALPH_SIZE; i++) {
> > if (tree[i].letter == letter) return i;
> > }
> >
> > throw new NoSuchElementException(" in findChar");
> > }
> >
> > /*
> > * HighestInBlock
> > *
> > * Returns the value of the node that is the highest in the block
> > * i.e. of all nodes with the same count
> > *
> > */
> >
> > public int highestInBlock (int count)
> > {
> >
> > int highest = -1;
> >
> > for (int i=0; i < 2*ALPH_SIZE; i++)
> > {
> > if (tree[i].count == count) highest=i;
> > }
> >
> > if (highest == -1) throw new NoSuchElementException("No such
node
> > with count of " + count);
> >
> > return highest;
> > }
> >
> >
> > public void updateTree(char newchar)
> > {
> > int current;
> > int max;
> >
> >
> > try { // first appearance
for
> > symbol
> > current = findChar(newchar); // Go to symbol
external
> > node
> >
> > // could enter catch block ?! yes
> >
> > max = highestInBlock(tree[current].count); // Node number max
in
> > block?
> >
> > // could enter catch block ?! probably not !
> >
> > if (current != max && tree[current].parent != max) {
> > addMessage(" Swapping nodes " +current+ " and " +max);
> > swap(current, max); // Switch node with
> > highest node in block
> > pause();
> > current = max;
> > }
> > addMessage(" Increasing count for '" +newchar+ "'");
> > tree[current].count++; // Increment node
weight
> > pause();
> >
> >
> > } catch (NoSuchElementException e) { // Yes
> > addMessage(" Spawning new node for '" +newchar+ "'");
> > current = spawn(newchar); // NYT gives birth
to
> > new NYT and external node
> > current = tree[current].parent; // Go to old NYT
node
> > tree[current].count++; // Increment count
of
> > old NYT node
> > pause();
> >
> > }
> >
> > while (current != root) // Is this the root
> > node?
> > {
> > current = tree[current].parent; // Go to parent node
> >
> > // no exception handling at all ?
> >
> > max = highestInBlock(tree[current].count); // Node number max
in
> > block?
> >
> > // no exception handling at all ?
> >
> > if (current != max && tree[current].parent != max) {
> > addMessage(" Swapping nodes " +current+ " and " +max);
> > swap(current, max); // Switch node with highest
node
> > in block
> > pause();
> > current = max;
> > }
> >
> > tree[current].count++; // Increment node
weight
> >
> > }
> >
> > drawTrie(dispRect, null);
> >
> > }
> >
> > Pretty lazy isn't it ?!?
> >
> > Bye,
> > Skybuck.
>
> 1. This is Java (ie wrong NG)

No because I was translating it to Delphi :D

> 2. No, it's pretty smart code. Notice that many of the methods has a
> return value. When they have a return value they can not also return a
> boolean. Instead they throw an exception, telling the caller that they
> have finished without a result. The exception tells the caller what
> the problem was, and this is handled by the caller (in the catch
> block).

This is a non-issue.

This is the way it should have been written ;)

( Besides from that I believe the java code actually has many potential
bugs... but let's not go into that... the code is probably ment only as an
example of the update tree algorithm... )

function FindCharacter( const Letter : Talphabet_letter; var Position :
integer ) : boolean;
var
 i : integer;
begin
 result := false;

 for i:=0 to (2 * const_alphabet_size)-1 do
 begin

  if (tree[i].letter = Letter) then
  begin
   Position := i;
   result := true;
   break;
  end;
 end;

end;

Bye,
  Skybuck.



Relevant Pages

  • Re: Example of misusage of exceptions.
    ... >> Skybuck. ... > boolean. ... Instead they throw an exception, telling the caller that they ...
    (comp.lang.java.programmer)
  • Unable to Write to Event Log w/ Enterprise Library 2.0 in Win App
    ... ExceptionPolicy.HandleException(ex, "Sample Exception Policy") ... keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, ... configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, ... context, String name, IConfigurationSource configurationSource, ...
    (microsoft.public.dotnet.general)
  • Re: urgent: beta 2.0 error
    ... Microsoft Online Support ... An unhandled exception has occurred. ... |> | Request URL: ... Boolean& ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: IIS 7 Catastrophic Failure :: COMException :: Microsoft.JScript
    ... Catastrophic Failure response on the first page request made. ... Catastrophic failure (Exception from HRESULT: ... System.RuntimeTypeHandle._GetTypeByName(String name, Boolean ...
    (microsoft.public.inetserver.iis)
  • Re: Sharepoint 2007 - Installation Error - Failed to register SP servi
    ... The issue stems from the installer going to a remote computer for SQL Server. ... Server I have the following exception: ... exception, Boolean breakConnection) ... RunBehavior runBehavior, String resetOptionsString) ...
    (microsoft.public.sharepoint.portalserver)