try except mechanism - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

try except mechanism

justin8justin8 Posts: 4Member
Hi,

Is there in perl some kind of an exception handling mechanism?
I want to have a mechanism in which when I open a file, and the file does not exist, then the script deals with this and notifices the user that the file can not be found. In python you have a try except mechanism, which works in this way


Thanks in advance...

Comments

  • JonathanJonathan Posts: 2,914Member
    Hi,

    : Is there in perl some kind of an exception handling mechanism?
    Uh...kinda. We have the eval structure:-

    [code]eval {
    # Throw an exception using die.
    die "Muhahahaha";
    };
    if ($@) {
    # An exception was throw.
    print "Caught exception: $@
    ";
    }[/code]

    If you raise an exception (using die) in an eval { ... } block, the error is stored in $@. If no error occurs it is certain to be null.

    : I want to have a mechanism in which when I open a file, and the file
    : does not exist, then the script deals with this and notifices the
    : user that the file can not be found. In python you have a try except
    : mechanism, which works in this way
    :
    Personally I'd just check the return value of open...

    [code]if (open FILE, "< path/to/file.txt") {
    # All is good, read stuff from the file, etc.
    } else {
    # File could not be opened.
    }[/code]

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.