Skip to content

Commit

Permalink
Merge pull request #209 from ankane/bad-any-cast3
Browse files Browse the repository at this point in the history
Improve exception handling for bad any cast
  • Loading branch information
jasonroelofs authored Oct 13, 2024
2 parents a82515b + 52bfddb commit 84e4071
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion include/rice/rice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,12 @@ namespace Rice::detail

if (k == klass && m == method_id)
{
return std::any_cast<Return_T>(d);
auto* ptr = std::any_cast<Return_T>(&d);
if (!ptr)
{
rb_raise(rb_eRuntimeError, "Unexpected return type for %s#%s", rb_class2name(klass), rb_id2name(method_id));
}
return *ptr;
}
}

Expand Down
7 changes: 6 additions & 1 deletion rice/detail/NativeRegistry.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ namespace Rice::detail

if (k == klass && m == method_id)
{
return std::any_cast<Return_T>(d);
auto* ptr = std::any_cast<Return_T>(&d);
if (!ptr)
{
rb_raise(rb_eRuntimeError, "Unexpected return type for %s#%s", rb_class2name(klass), rb_id2name(method_id));
}
return *ptr;
}
}

Expand Down

0 comments on commit 84e4071

Please sign in to comment.