[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Regexp rescue method

Brian Candler

6/28/2007 9:30:00 AM

> I can't catch it even with rescue Object... wow.

That's not surprising. Malformed regexps are a *parse* time error. Parsing
is what builds the Annotated Syntax Tree which Ruby runs. No Ruby code can
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 parentheses
or 'end' statements.

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

$ cat a.rb
regexp = /[/
$ cat b.rb
begin
require 'a'
rescue SyntaxError
puts "Darn."
end
$ ruby b.rb
Darn.