2

s = "\u{d800}" produces an error, test.rb:1: invalid Unicode codepoint, as expected. However,

begin
  s = "\u{d800}"
rescue Exception
  puts "oh no"
end

does the same thing, producing the error rather than an output of oh no. Is there a built-in way to rescue this, or would I have to hardcode valid ranges of Unicode codepoints to check validity?

Nnnes
  • 213
  • 2
  • 8
  • I can confirm `SyntaxError` in 2.5.1 unfortunately a `SyntaxError` cannot be rescued in that fashion the only way you could rescue this error would be to "eval" that code e.g. `begin; eval('"\u{d800}"'); rescue SyntaxError; 'oh no';end;` but I would not recommend this. Can you explain a bit further how this is happening? – engineersmnky Jun 25 '18 at 17:10
  • Unrelated, but never rescue `Exception`. Always rescue something more specific https://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – Max Jun 25 '18 at 17:49

1 Answers1

1

You can test if a number is a valid unicode codepoint with chr

0xd800.chr('UTF-8')
# RangeError (invalid codepoint 0xd800 in UTF-8)
Max
  • 21,123
  • 5
  • 49
  • 71