trapping compile time errors

Discussion in 'Ruby' started by Gerard A.W. Vreeswijk, Jul 7, 2003.

  1. 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
    %&^&$%
    ^
    >Exit code: 1 Time: 0.229

    ===============================================================
     
    Gerard A.W. Vreeswijk, Jul 7, 2003
    #1
    1. Advertising

  2. On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
    > I want to catch compile time errors with my own method.

    ...
    > Eventually,
    > I came up with the following, which is not as I imagined. It's ugly.


    It's correct though - need an 'eval' because you must have a working
    (syntactically-valid) program A to report the error, before attempting to
    compile invalid program B.

    The other way is with require or load:

    begin
    require 'somefile.rb'
    rescue Exception => e
    puts "Error here (#{e}). May be any error, including SyntaxError"
    end

    Regards,

    Brian.
     
    Brian Candler, Jul 7, 2003
    #2
    1. Advertising

  3. On Mon, 7 Jul 2003 17:19:26 +0900, Brian Candler <>
    wrote:

    >On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
    >> I want to catch compile time errors with my own method.

    >..
    >> Eventually,
    >> I came up with the following, which is not as I imagined. It's ugly.

    >
    >It's correct though - need an 'eval' because you must have a working
    >(syntactically-valid) program A to report the error, before attempting to
    >compile invalid program B.


    Allright. This is good. Still, I hope it is possible to report
    syntax errors other than by eval. Or am I aiming for the impossible?
    (In Perl it is possible anyway.)

    Why I want it is to report all errors including syntax to the browser.
    Such errors must always be preceded by a "Content-type: text/html\n\n"

    I don't want to use the CGI module, because I consider this to be
    overkill for the small script I am writing.
     
    Gerard A.W. Vreeswijk, Jul 7, 2003
    #3
  4. On Mon, Jul 07, 2003 at 08:24:45AM +0000, Gerard A.W. Vreeswijk wrote:
    > On Mon, 7 Jul 2003 17:19:26 +0900, in comp.lang.ruby you wrote:
    >
    > >On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
    > >> I want to catch compile time errors with my own method.

    > >..
    > >> Eventually,
    > >> I came up with the following, which is not as I imagined. It's ugly.

    > >
    > >It's correct though - need an 'eval' because you must have a working
    > >(syntactically-valid) program A to report the error, before attempting to
    > >compile invalid program B.
    > >
    > >The other way is with require or load:
    > >
    > >begin
    > > require 'somefile.rb'
    > >rescue Exception => e
    > > puts "Error here (#{e}). May be any error, including SyntaxError"
    > >end
    > >
    > >Regards,
    > >
    > >Brian.

    >
    >
    > Thanks Brian. Still, I would not be surprised if there is a solution
    > by means of opening Excpetion class.


    No, there cannot be such a solution.

    Ruby runs programs by
    (1) parsing the code, and converting to an Annotated Syntax Tree
    (2) running the AST

    If step 1 fails, no code will run at all.

    Brian.
     
    Brian Candler, Jul 7, 2003
    #4
  5. Re: trapping compile time errors -- alas

    On Mon, 7 Jul 2003 18:38:03 +0900, Brian Candler <>
    wrote:

    >No, there cannot be such a solution.
    >
    >Ruby runs programs by
    >(1) parsing the code, and converting to an Annotated Syntax Tree
    >(2) running the AST
    >
    >If step 1 fails, no code will run at all.
    >
    >Brian.
    >


    I am convinced. So Ruby BEGIN differs from Perl BEGIN. Alas I'd say:
    check tail of http://www.oreilly.com/catalog/pperl3/chapter/ch18.html
    Compare:

    modena:gv/klad-: cat try
    BEGIN {
    print "In Perl will be parsed and run first, irrespective of parse
    errors past BEGIN\n";
    }

    # Syntax errors.
    %&^&$%
    modena:gv/klad-: ruby try
    try:6: parse error
    %&^&$%
    ^
    modena:gv/klad-: perl try
    In Perl will be parsed and run first, irrespective of parse errors past
    BEGIN
    syntax error at try line 6, at EOF
    Execution of try aborted due to compilation errors.
    modena:gv/klad-:
     
    Gerard A.W. Vreeswijk, Jul 7, 2003
    #5
  6. "Gerard A.W. Vreeswijk" <> schrieb im Newsbeitrag
    news:...
    > On Mon, 7 Jul 2003 17:19:26 +0900, Brian Candler <>
    > wrote:
    >
    > >On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
    > >> I want to catch compile time errors with my own method.

    > >..
    > >> Eventually,
    > >> I came up with the following, which is not as I imagined. It's ugly.

    > >
    > >It's correct though - need an 'eval' because you must have a working
    > >(syntactically-valid) program A to report the error, before attempting

    to
    > >compile invalid program B.

    >
    > Allright. This is good. Still, I hope it is possible to report
    > syntax errors other than by eval. Or am I aiming for the impossible?
    > (In Perl it is possible anyway.)
    >
    > Why I want it is to report all errors including syntax to the browser.
    > Such errors must always be preceded by a "Content-type: text/html\n\n"
    >
    > I don't want to use the CGI module, because I consider this to be
    > overkill for the small script I am writing.


    You can

    - catch exceptions from require (as Brian pointed out).

    - replace STDERR (or $stderr) by a StringOut (maybe it has a different
    name) and see whether it contains something in which case you report it
    back to the client.

    You don't need to fuddle with class Exception at all.

    Apart from that: Syntax errors in skript are normally program errors which
    you don't want to report to the client because they should not occur after
    testing; if they do occur, an exception dump and / or a log entry is good
    enough.

    Of course, if a user may provide a script that you want to execute he
    want's to get informed of such errors. But this is - as you might have
    guessed - a dangerous thing to do...

    Regards

    robert
     
    Robert Klemme, Jul 7, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbA==?=

    Trapping 'compile' errors when aspx page is rendered.

    =?Utf-8?B?QmlsbA==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    535
    =?Utf-8?B?QmlsbA==?=
    Mar 29, 2005
  2. Aaron Fude

    Trapping JNI errors

    Aaron Fude, Jun 1, 2004, in forum: Java
    Replies:
    4
    Views:
    1,513
    Gordon Beaton
    Jun 2, 2004
  3. Avi Kak
    Replies:
    6
    Views:
    328
    Duncan Booth
    Feb 13, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    901
    Lionel B
    Mar 1, 2007
  5. Carter
    Replies:
    2
    Views:
    522
    Carter
    Mar 4, 2009
Loading...

Share This Page