G
Gerard A.W. Vreeswijk
Hi,
I want to catch compile time errors with my own method.
In Perl, you can do this:
$SIG{__DIE__} = \¨
sub die {
print "Error here. May be any error, including SyntaxError\n";
exit 1;
}
I.e., you can trap die and define your own. Does anyone know how to do
this in Ruby? I have tried opening up Exception class and redefining
some of the methods there. No success. Eventually,
I came up with the following, which is not as I imagined. It's ugly.
I am sure there exist better ways but currently I ran out of ideas.
Does anyone have better ideas? Thanks.
===============================================================
#!/usr/bin/ruby
begin
# From Pickaxe book:
# eval( aString [, aBinding [ file [ line ] ] ]) -> anObject
# Evaluates the Ruby expression(s) in aString. If aBinding is
# given, the evaluation is performed in its context. The
# binding may be a Binding object or a Proc object. If the
# optional file and line parameters are present, they will
# be used when reporting syntax errors.
eval DATA.gets(nil), nil, __FILE__, DATA.lineno
# gets(nil), because we want to set the line separator to be
# nil so that entire DATA is slurped into one big string.
rescue Exception
puts "Error here. May be any error, including SyntaxError"
# Redirect STDERR to STDOUT
STDERR.reopen(STDOUT)
# re-raise error. Now appears on STDOUT
raise
end
__END__
# Your program here, including possible syntax errors.
%&^&$%
===============================================================
Outputs:
Error here. May be any error, including SyntaxError
try.rb:13: compile error (SyntaxError)
try.rb:13: parse error
%&^&$%
^
I want to catch compile time errors with my own method.
In Perl, you can do this:
$SIG{__DIE__} = \¨
sub die {
print "Error here. May be any error, including SyntaxError\n";
exit 1;
}
I.e., you can trap die and define your own. Does anyone know how to do
this in Ruby? I have tried opening up Exception class and redefining
some of the methods there. No success. Eventually,
I came up with the following, which is not as I imagined. It's ugly.
I am sure there exist better ways but currently I ran out of ideas.
Does anyone have better ideas? Thanks.
===============================================================
#!/usr/bin/ruby
begin
# From Pickaxe book:
# eval( aString [, aBinding [ file [ line ] ] ]) -> anObject
# Evaluates the Ruby expression(s) in aString. If aBinding is
# given, the evaluation is performed in its context. The
# binding may be a Binding object or a Proc object. If the
# optional file and line parameters are present, they will
# be used when reporting syntax errors.
eval DATA.gets(nil), nil, __FILE__, DATA.lineno
# gets(nil), because we want to set the line separator to be
# nil so that entire DATA is slurped into one big string.
rescue Exception
puts "Error here. May be any error, including SyntaxError"
# Redirect STDERR to STDOUT
STDERR.reopen(STDOUT)
# re-raise error. Now appears on STDOUT
raise
end
__END__
# Your program here, including possible syntax errors.
%&^&$%
===============================================================
Outputs:
Error here. May be any error, including SyntaxError
try.rb:13: compile error (SyntaxError)
try.rb:13: parse error
%&^&$%
^
===============================================================Exit code: 1 Time: 0.229