Project

General

Profile

Actions

Bug #13564

closed

Exception message management

Added by ko1 (Koichi Sasada) almost 7 years ago. Updated over 4 years ago.

Status:
Rejected
Target version:
-
[ruby-core:81158]

Description

We can modify Exception#message if given String is not frozen.
Should we continue this specification?

Now, we can modify exception message with message string modification.

begin
  raise 'foo'
ensure
  $!.message.replace 'bar' #=> Exception with message "bar"
end

However, if we pass frozen string, it is not allowed.

begin
  raise 'foo'.freeze
ensure
  $!.message.replace 'bar' #=> `replace': can't modify frozen String (RuntimeError)
end

Furthermore, # frozen_string_literal: true freeze all of string literals.

# frozen_string_literal: true

begin
  raise 'foo'
ensure
  $!.message.replace 'bar' #=> `replace': can't modify frozen String (RuntimeError)
end

Background and motivation

I want to add Exception message on ensure clause like the code in previous section. Just now, we need to re-raise another exception (with raise($!.class, new_msg, $!.backtrace)). I tried to modify $!.message and it works on small script. However, I try it on production (*1), it doesn't work because of frozen_string_literal: true.

*1: https://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/rubygems/test_gem_gem_runner.rb?r1=58723&r2=58722&pathrev=58723

I think current behavior (specification?) is easy to misusing.

Ideas

  • (1) To prevent such behavior
    • (1-1) Freeze message strings at initialize
    • (1-2) Return copy string at Exception#message
  • (2) Provide Exception#message =
    • And (1-1) or (1-2)
  • (3) Allow such behavior. If a frozen message is given, dup it and set as modifiable.
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0