# Regexp rescue method

**URL:** <https://rubytalk.org/t/regexp-rescue-method/38694>\
**Category:** ruby-talk\
**Created:** [28 June 2007 09:29 UTC](https://rubytalk.org/t/regexp-rescue-method/38694 "2007-06-28T09:29:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [28 June 2007 09:29 UTC](https://rubytalk.org/t/regexp-rescue-method/38694/1 "2007-06-28T09:29:38Z")

</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  
&nbsp;&nbsp;require 'a'  
rescue SyntaxError  
&nbsp;&nbsp;puts "Darn."  
end  
$ ruby b.rb  
Darn.
