# TypeError maps to System.InvalidOperationException?

**URL:** <https://www.ruby-forum.com/t/typeerror-maps-to-system-invalidoperationexception/158321>\
**Category:** IronRuby\
**Created:** [January 22, 2009, 1:52am UTC](https://www.ruby-forum.com/t/typeerror-maps-to-system-invalidoperationexception/158321 "2009-01-22T01:52:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [January 22, 2009, 1:52am UTC](https://www.ruby-forum.com/t/typeerror-maps-to-system-invalidoperationexception/158321/1 "2009-01-22T01:52:49Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [January 22, 2009, 4:20am UTC](https://www.ruby-forum.com/t/typeerror-maps-to-system-invalidoperationexception/158321/2 "2009-01-22T04:20:25Z")

</div>

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](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](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

---

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [January 22, 2009, 7:24am UTC](https://www.ruby-forum.com/t/typeerror-maps-to-system-invalidoperationexception/158321/3 "2009-01-22T07:24:36Z")

</div>

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](http://www.ironruby.net/Documentation/CLR_Interop/Specification).

From: removed\_email\_address@domain.invalid  
[[mailto: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](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](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

---

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [January 22, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/typeerror-maps-to-system-invalidoperationexception/158321/4 "2009-01-22T20:26:30Z")

</div>

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](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](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](http://www.ironruby.net/Documentation/CLR_Interop/Specification).

From: removed\_email\_address@domain.invalid  
[[mailto: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](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](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

---

<div class="post-metadata">

**Author:** ![Shri\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Shri\_B](https://www.ruby-forum.com/u/Shri_B)\
**Post date:** [January 22, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/typeerror-maps-to-system-invalidoperationexception/158321/5 "2009-01-22T20:20:01Z")

</div>

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](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](http://www.ironruby.net/Documentation/CLR_Interop/Specification).

From: removed\_email\_address@domain.invalid  
[[mailto: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](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](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
