Forum: Ruby Re: Regexp rescue method

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-06-28 11:38
(Received via mailing list)
> I can't catch it even with rescue Object... wow.

That's not surprising. Malformed regexps are a *parse* time error.
is what builds the Annotated Syntax Tree which Ruby runs. No Ruby code
be executed until the AST has been built, i.e. the whole file has been
parsed successfully, and that includes your exception handler of course.

The same applies to other structural errors, such as mismatched
or 'end' statements.

If you put the code into another file and load it using 'require' or
then you can catch the error:

$ cat a.rb
regexp = /[/
$ cat b.rb
  require 'a'
rescue SyntaxError
  puts "Darn."
$ ruby b.rb
This topic is locked and can not be replied to.