# Re: Regexp rescue method

**URL:** <https://www.ruby-forum.com/t/re-regexp-rescue-method/108009>\
**Category:** Ruby\
**Created:** [June 28, 2007, 11:38am UTC](https://www.ruby-forum.com/t/re-regexp-rescue-method/108009 "2007-06-28T11:38:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [June 28, 2007, 11:38am UTC](https://www.ruby-forum.com/t/re-regexp-rescue-method/108009/1 "2007-06-28T11:38:56Z")

</div>

> 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.
