Archived

This forum has been archived. Please start a new discussion on GitHub.

Exception throwing question

Hi,


Explanation:
I have a small question about Exception throwing...
I took the example in ICE\examples\book\filesystem and modified it a little:
module Filesystem {
    exception Error { };
    
    exception GenericError
      extends Error
    {
    	string reason;
    };

    interface Node {
    	nonmutating string name();
    };

    sequence<string> Lines;

    interface File extends Node {
    	nonmutating Lines read();
	idempotent void write(Lines text) throws Error;
    };

    sequence<Node*> NodeSeq;

    interface Directory extends Node {
    	nonmutating NodeSeq list();
    };
};

As you can see, write throws an "Error" [before GenericError], where Error is the base-exception of GenericError.

I modified the code a little to whenever something is 'write'-en to a File, it throws a "GenericError". At the server-side, I just capture it and don't handle it...

At the clientside whenever I do a read i do this:
Lines text = file->read();
      try
      {
        file->write(text);
      }
      catch ( const GenericError& e )
      {
        cerr << e << endl;    	
      }
      catch( const Error& e)
      {
        cerr << e << endl;    	
      }

Both server and client run on the same system (maybe that's important to know).

The question is here about - maybe - an inconsistency in the documentation (p. 101-102):
An operation can throw only those user exceptions that are listed in its exception specification. If, at run time, the implementation of an operation throws an exception that is not listed in its exception specification, the client receives a run-time exception (see Section 4.10.4) to indicate that the operation did something illegal.

The question:
What I think is not correct is that I said - in Slice - the function throws "Error", but the client receives it as a "GenericError". Ofcourse this is perfect for me... So I can just create my exception-structure and instead of telling like 20 possible exceptions a function can throw (especially for the higher functions 'up the chain'), I could just tell the base-exception...
I understand it would be a better practice to show them all, but maybe I could just show the most reasonable ones + the base exception.

If I remove the "throws Error" completely, it will generate an "UnknownUserException" at the clientside with the ice_name() "Filesystem::GenericError".


Could someone clarify this to me?

Thanks!

Comments

  • In "Distributed Programming with Ice" (a.k.a the Ice docs) the section on "Exception Inheritance" contains the text:
    Note that, if the exception specification of an operation indicates a specific exception type, at run time, the implementation of the operation may also throw more derived exceptions.

    So what you are seeing is correct, and I think you'll agree, desirable.
  • Thanks :)

    And indeed... It's desirable :)