TypeError maps to System.InvalidOperationException?


#1

We map Ruby’s TypeError to System.InvalidOperationException. This
surprised me. I would have expected TypeError to map to
System.InvalidCastException. Wouldn’t that be preferable?

In general, it would be good to document on
http://www.ironruby.net/Documentation/CLR_Interop/Specification the
exact mapping of the entire Ruby exception hierarchy to the equivalent
.NET exception types. This will be useful documentation, especially for
apps that host IronRuby scripts, and we can also do a brief review and
potentially tweak the mapping. I have added a table at the bottom of the
page which is half filled in.

Thanks,
Shri


#2

If you search for “RubyExceptions.CreateTypeError” in libraries you’ll
get lines including the following (apart from type conversion errors).
So it’s not always a cast or conversion that fails. On the other hand, a
conversion to a wrong type is an invalid operation. The exception
mapping is attached.

C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\MutableString.cs(244):
throw RubyExceptions.CreateTypeError(“can’t modify frozen object”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(171):
throw RubyExceptions.CreateTypeError(“can’t copy singleton class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(183):
throw RubyExceptions.CreateTypeError(“already initialized class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(510):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(592):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyModule.cs(355):
throw RubyExceptions.CreateTypeError(“can’t modify frozen Module”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\Globals\SpecialGlobalVariableInfo.cs(239):
throw RubyExceptions.CreateTypeError(String.Format("${0} must have write
method, {1} given",
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1321):
throw RubyExceptions.CreateTypeError(“assigning non-exception to $!”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1345):
throw RubyExceptions.CreateTypeError(“backtrace must be Array of
String”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyExceptions.cs(127):
return RubyExceptions.CreateTypeError(String.Format("{0} can’t be
coerced into {1}", selfClass, otherClass));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyOps.cs(503):
throw RubyExceptions.CreateTypeError(“can’t define singleton method for
literals”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(224):
throw RubyExceptions.CreateTypeError(“unable to marshal time”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(260):
throw RubyExceptions.CreateTypeError(“marshaled time format differ”);

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Shri B.
Sent: Wednesday, January 21, 2009 4:47 PM
To: removed_email_address@domain.invalid
Subject: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

We map Ruby’s TypeError to System.InvalidOperationException. This
surprised me. I would have expected TypeError to map to
System.InvalidCastException. Wouldn’t that be preferable?

In general, it would be good to document on
http://www.ironruby.net/Documentation/CLR_Interop/Specification the
exact mapping of the entire Ruby exception hierarchy to the equivalent
.NET exception types. This will be useful documentation, especially for
apps that host IronRuby scripts, and we can also do a brief review and
potentially tweak the mapping. I have added a table at the bottom of the
page which is half filled in.

Thanks,
Shri


#3

All exceptions are invalid operations of some sort, and you could
potentially want to use InvalidOperationException for any Ruby exception
which did not map well to a CLR exception. I think picking either
InvalidCastException or TypeLoadException would be better as it would be
a bit more specific. And an InvalidCastException caused in C# code would
be displayed as a TypeError in Ruby which would be nice. But given that
neither InvalidCastException or TypeLoadException are a perfect match
for TypeError, I won’t push on this if everyone is OK with the current
mapping.

Btw, the image is cool. I have attached it to
http://www.ironruby.net/Documentation/CLR_Interop/Specification.

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Tomas M.
Sent: Wednesday, January 21, 2009 6:36 PM
To: removed_email_address@domain.invalid
Subject: Re: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

If you search for “RubyExceptions.CreateTypeError” in libraries you’ll
get lines including the following (apart from type conversion errors).
So it’s not always a cast or conversion that fails. On the other hand, a
conversion to a wrong type is an invalid operation. The exception
mapping is attached.

C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\MutableString.cs(244):
throw RubyExceptions.CreateTypeError(“can’t modify frozen object”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(171):
throw RubyExceptions.CreateTypeError(“can’t copy singleton class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(183):
throw RubyExceptions.CreateTypeError(“already initialized class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(510):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(592):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyModule.cs(355):
throw RubyExceptions.CreateTypeError(“can’t modify frozen Module”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\Globals\SpecialGlobalVariableInfo.cs(239):
throw RubyExceptions.CreateTypeError(String.Format("${0} must have write
method, {1} given",
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1321):
throw RubyExceptions.CreateTypeError(“assigning non-exception to $!”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1345):
throw RubyExceptions.CreateTypeError(“backtrace must be Array of
String”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyExceptions.cs(127):
return RubyExceptions.CreateTypeError(String.Format("{0} can’t be
coerced into {1}", selfClass, otherClass));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyOps.cs(503):
throw RubyExceptions.CreateTypeError(“can’t define singleton method for
literals”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(224):
throw RubyExceptions.CreateTypeError(“unable to marshal time”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(260):
throw RubyExceptions.CreateTypeError(“marshaled time format differ”);

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Shri B.
Sent: Wednesday, January 21, 2009 4:47 PM
To: removed_email_address@domain.invalid
Subject: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

We map Ruby’s TypeError to System.InvalidOperationException. This
surprised me. I would have expected TypeError to map to
System.InvalidCastException. Wouldn’t that be preferable?

In general, it would be good to document on
http://www.ironruby.net/Documentation/CLR_Interop/Specification the
exact mapping of the entire Ruby exception hierarchy to the equivalent
.NET exception types. This will be useful documentation, especially for
apps that host IronRuby scripts, and we can also do a brief review and
potentially tweak the mapping. I have added a table at the bottom of the
page which is half filled in.

Thanks,
Shri


#4

Makes sense. I think we can map it to InvalidCastException. The core
implementations of built-in classes such as Hash, RubyArray, etc. could
still throw InvalidOperationException if there is a problem with the
object state and the library wrappers can rethrow it as InvalidCast
(TypeError) to match Ruby error type. C# apps using Ruby built-in
classes will get InvalidOperation, which is what they would expect.

Tomas

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Shri B.
Sent: Thursday, January 22, 2009 11:10 AM
To: removed_email_address@domain.invalid
Subject: Re: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

Btw, one more consideration is that if a .NET method throws
InvalidOperationException, IronRuby will map it to TypeError even though
the .NET method was not doing anything at all with the type system. This
will be confusing to users. So it would be preferable to map TypeError
to InvalidCastException or TypeLoadException, or create a new
IronRuby.Builtins.TypeError exception type.

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Shri B.
Sent: Wednesday, January 21, 2009 10:04 PM
To: removed_email_address@domain.invalid
Subject: Re: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

All exceptions are invalid operations of some sort, and you could
potentially want to use InvalidOperationException for any Ruby exception
which did not map well to a CLR exception. I think picking either
InvalidCastException or TypeLoadException would be better as it would be
a bit more specific. And an InvalidCastException caused in C# code would
be displayed as a TypeError in Ruby which would be nice. But given that
neither InvalidCastException or TypeLoadException are a perfect match
for TypeError, I won’t push on this if everyone is OK with the current
mapping.

Btw, the image is cool. I have attached it to
http://www.ironruby.net/Documentation/CLR_Interop/Specification.

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Tomas M.
Sent: Wednesday, January 21, 2009 6:36 PM
To: removed_email_address@domain.invalid
Subject: Re: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

If you search for “RubyExceptions.CreateTypeError” in libraries you’ll
get lines including the following (apart from type conversion errors).
So it’s not always a cast or conversion that fails. On the other hand, a
conversion to a wrong type is an invalid operation. The exception
mapping is attached.

C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\MutableString.cs(244):
throw RubyExceptions.CreateTypeError(“can’t modify frozen object”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(171):
throw RubyExceptions.CreateTypeError(“can’t copy singleton class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(183):
throw RubyExceptions.CreateTypeError(“already initialized class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(510):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(592):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyModule.cs(355):
throw RubyExceptions.CreateTypeError(“can’t modify frozen Module”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\Globals\SpecialGlobalVariableInfo.cs(239):
throw RubyExceptions.CreateTypeError(String.Format("${0} must have write
method, {1} given",
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1321):
throw RubyExceptions.CreateTypeError(“assigning non-exception to $!”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1345):
throw RubyExceptions.CreateTypeError(“backtrace must be Array of
String”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyExceptions.cs(127):
return RubyExceptions.CreateTypeError(String.Format("{0} can’t be
coerced into {1}", selfClass, otherClass));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyOps.cs(503):
throw RubyExceptions.CreateTypeError(“can’t define singleton method for
literals”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(224):
throw RubyExceptions.CreateTypeError(“unable to marshal time”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(260):
throw RubyExceptions.CreateTypeError(“marshaled time format differ”);

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Shri B.
Sent: Wednesday, January 21, 2009 4:47 PM
To: removed_email_address@domain.invalid
Subject: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

We map Ruby’s TypeError to System.InvalidOperationException. This
surprised me. I would have expected TypeError to map to
System.InvalidCastException. Wouldn’t that be preferable?

In general, it would be good to document on
http://www.ironruby.net/Documentation/CLR_Interop/Specification the
exact mapping of the entire Ruby exception hierarchy to the equivalent
.NET exception types. This will be useful documentation, especially for
apps that host IronRuby scripts, and we can also do a brief review and
potentially tweak the mapping. I have added a table at the bottom of the
page which is half filled in.

Thanks,
Shri


#5

Btw, one more consideration is that if a .NET method throws
InvalidOperationException, IronRuby will map it to TypeError even though
the .NET method was not doing anything at all with the type system. This
will be confusing to users. So it would be preferable to map TypeError
to InvalidCastException or TypeLoadException, or create a new
IronRuby.Builtins.TypeError exception type.

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Shri B.
Sent: Wednesday, January 21, 2009 10:04 PM
To: removed_email_address@domain.invalid
Subject: Re: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

All exceptions are invalid operations of some sort, and you could
potentially want to use InvalidOperationException for any Ruby exception
which did not map well to a CLR exception. I think picking either
InvalidCastException or TypeLoadException would be better as it would be
a bit more specific. And an InvalidCastException caused in C# code would
be displayed as a TypeError in Ruby which would be nice. But given that
neither InvalidCastException or TypeLoadException are a perfect match
for TypeError, I won’t push on this if everyone is OK with the current
mapping.

Btw, the image is cool. I have attached it to
http://www.ironruby.net/Documentation/CLR_Interop/Specification.

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Tomas M.
Sent: Wednesday, January 21, 2009 6:36 PM
To: removed_email_address@domain.invalid
Subject: Re: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

If you search for “RubyExceptions.CreateTypeError” in libraries you’ll
get lines including the following (apart from type conversion errors).
So it’s not always a cast or conversion that fails. On the other hand, a
conversion to a wrong type is an invalid operation. The exception
mapping is attached.

C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\MutableString.cs(244):
throw RubyExceptions.CreateTypeError(“can’t modify frozen object”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(171):
throw RubyExceptions.CreateTypeError(“can’t copy singleton class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(183):
throw RubyExceptions.CreateTypeError(“already initialized class”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(510):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyClass.cs(592):
throw RubyExceptions.CreateTypeError(String.Format(“allocator undefined
for {0}”, Name));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Builtins\RubyModule.cs(355):
throw RubyExceptions.CreateTypeError(“can’t modify frozen Module”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\Globals\SpecialGlobalVariableInfo.cs(239):
throw RubyExceptions.CreateTypeError(String.Format("${0} must have write
method, {1} given",
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1321):
throw RubyExceptions.CreateTypeError(“assigning non-exception to $!”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyContext.cs(1345):
throw RubyExceptions.CreateTypeError(“backtrace must be Array of
String”);
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyExceptions.cs(127):
return RubyExceptions.CreateTypeError(String.Format("{0} can’t be
coerced into {1}", selfClass, otherClass));
C:\M0\Merlin\Main\Languages\Ruby\Ruby\Runtime\RubyOps.cs(503):
throw RubyExceptions.CreateTypeError(“can’t define singleton method for
literals”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(224):
throw RubyExceptions.CreateTypeError(“unable to marshal time”);
C:\M0\Merlin\Main\Languages\Ruby\Libraries.LCA_RESTRICTED\Builtins\TimeOps.cs(260):
throw RubyExceptions.CreateTypeError(“marshaled time format differ”);

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Shri B.
Sent: Wednesday, January 21, 2009 4:47 PM
To: removed_email_address@domain.invalid
Subject: [Ironruby-core] TypeError maps to
System.InvalidOperationException?

We map Ruby’s TypeError to System.InvalidOperationException. This
surprised me. I would have expected TypeError to map to
System.InvalidCastException. Wouldn’t that be preferable?

In general, it would be good to document on
http://www.ironruby.net/Documentation/CLR_Interop/Specification the
exact mapping of the entire Ruby exception hierarchy to the equivalent
.NET exception types. This will be useful documentation, especially for
apps that host IronRuby scripts, and we can also do a brief review and
potentially tweak the mapping. I have added a table at the bottom of the
page which is half filled in.

Thanks,
Shri