Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Can I catch exceptions

by richmusk (Novice)
on Jun 26, 2001 at 13:08 UTC ( [id://91534]=perlquestion: print w/replies, xml ) Need Help??

richmusk has asked for the wisdom of the Perl Monks concerning the following question:

I have a line of code in my program the compares two values. One of these values can be a string or an integer. I want to compare the integers and generate some sort of error message if a string is found, but a the moment I get the following error message
Argument "Offset1" isn't numeric in eq at code.pl line 199, chunk 1233 +.
Is there any way I can catch that error and generate my own message ???

Replies are listed 'Best First'.
Re: Can I catch exceptions
by iakobski (Pilgrim) on Jun 26, 2001 at 15:30 UTC
    The first two answers are correct answers to your question, however they will not solve your problem.

    What you are seeing is not an exception or an error, it is a warning. A subtle difference perhaps, but the eval and die technique will not trap warnings.

    What you need to do is to temporarily modify __WARN__ so that instead of the default, which is to print out the warning, it does what you want. You say that you want to catch the "error" and generate your own message. You could do something like this:

    sub my_comparison{ @_ == 2 or die "Incorrect number of args to my_comparison"; local $SIG{__WARN__} = sub { print "Tried to test a non-number with +==\n" }; local $^W = 1; $_[0] == $_[1]; }
    Note that you MUST enclose this in a block (such as putting it in it's own sub like this) otherwise you will be confused by any other warnings later printing your own message.

    -- iakobski

Re: Can I catch exceptions
by Masem (Monsignor) on Jun 26, 2001 at 15:50 UTC
    Another option besides modifying warn is simply to check for the number-ness of the values before using the == operation, such that you can control what warnings are printed. For example:
    if( $num1 !~ /^\d+$/ ) { warn "First value is not a number"; } elsif ( $num2 !~ /^\d+$/ ) { warn "Second value is not a number"; } else { my $result = ( $num1 == $num2 ); }
    Note that the regex used above checks for whole numbers; several more useful regexes for other number types are listed in perlfaq4, under the question "How do I determine whether a scalar is a number/whole/integer/float?"


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Can I catch exceptions
by Chady (Priest) on Jun 26, 2001 at 13:47 UTC

    try a combination of eval and die for that. ex:

    eval { if .... { die 'Error Message 1'; else { die 'Error Message 2'; } };
    then you check the value of $@ to get the 'death' message, and do your deed.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: Can I catch exceptions
by azatoth (Curate) on Jun 26, 2001 at 13:10 UTC
(tye)Re: Can I catch exceptions
by tye (Sage) on Jun 26, 2001 at 18:42 UTC

    Just turn off warnings for that bit of code:

    if( do{local($^W)=0; $x == $y } ) { # ...

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://91534]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 19:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found