From b57915eddc91ce0369ae8bcf82d8c4364f42ea05 Mon Sep 17 00:00:00 2001 From: shyouhei Date: Tue, 12 Dec 2017 00:46:34 +0000 Subject: Add FrozenError as a subclass of RuntimeError FrozenError will be used instead of RuntimeError for exceptions raised when there is an attempt to modify a frozen object. The reason for this change is to differentiate exceptions related to frozen objects from generic exceptions such as those generated by Kernel#raise without an exception class. From: Jeremy Evans Signed-off-by: Urabe Shyouhei git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61131 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- error.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'error.c') diff --git a/error.c b/error.c index f00951940f..d257028ad2 100644 --- a/error.c +++ b/error.c @@ -804,6 +804,7 @@ VALUE rb_eSignal; VALUE rb_eFatal; VALUE rb_eStandardError; VALUE rb_eRuntimeError; +VALUE rb_eFrozenError; VALUE rb_eTypeError; VALUE rb_eArgError; VALUE rb_eIndexError; @@ -2011,16 +2012,21 @@ syserr_eqq(VALUE self, VALUE exc) */ /* - * Document-class: RuntimeError + * Document-class: FrozenError * - * A generic error class raised when an invalid operation is attempted. + * Raised when there is an attempt to modify a frozen object. * * [1, 2, 3].freeze << 4 * * raises the exception: * - * RuntimeError: can't modify frozen Array + * FrozenError: can't modify frozen Array + */ + +/* + * Document-class: RuntimeError * + * A generic error class raised when an invalid operation is attempted. * Kernel#raise will raise a RuntimeError if no Exception class is * specified. * @@ -2233,6 +2239,7 @@ Init_Exception(void) rb_define_method(rb_eNoMethodError, "private_call?", nometh_err_private_call_p, 0); rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError); + rb_eFrozenError = rb_define_class("FrozenError", rb_eRuntimeError); rb_eSecurityError = rb_define_class("SecurityError", rb_eException); rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException); rb_eEncodingError = rb_define_class("EncodingError", rb_eStandardError); @@ -2588,7 +2595,7 @@ rb_load_fail(VALUE path, const char *err) void rb_error_frozen(const char *what) { - rb_raise(rb_eRuntimeError, "can't modify frozen %s", what); + rb_raise(rb_eFrozenError, "can't modify frozen %s", what); } void @@ -2601,11 +2608,11 @@ rb_error_frozen_object(VALUE frozen_obj) VALUE path = rb_ary_entry(debug_info, 0); VALUE line = rb_ary_entry(debug_info, 1); - rb_raise(rb_eRuntimeError, "can't modify frozen %"PRIsVALUE", created at %"PRIsVALUE":%"PRIsVALUE, + rb_raise(rb_eFrozenError, "can't modify frozen %"PRIsVALUE", created at %"PRIsVALUE":%"PRIsVALUE, CLASS_OF(frozen_obj), path, line); } else { - rb_raise(rb_eRuntimeError, "can't modify frozen %"PRIsVALUE, + rb_raise(rb_eFrozenError, "can't modify frozen %"PRIsVALUE, CLASS_OF(frozen_obj)); } } -- cgit v1.2.3