CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
problem in error handling.

 



ajitnavasare
Novice

Nov 20, 2009, 4:17 AM

Post #1 of 5 (3728 views)
problem in error handling. Can't Post

I have a problem in error handling.

I want to stop the script as soon as it get an error and stop further execution.
In my below “sample script”, step 1 throws an error, and still step 2 get executed.
Can any one help me out, I want to do it in the following manner.
As on step 1 get error I want to stop the execution of script.

i think i am not using(or not aware of ) proper perl error handling features Unimpressed

Sample Script :
### Script : Start
use strict;
use warnings;
use Sys::HostIP;
use Win32::GuiTest qw[:ALL];
eval
{
system("start c:\\windows\\system32\\cmd1212.exe"); ### step 1
SendKeys(" dir {ENTER}"); ### Step 2
sleep 3;
};
if($@)
{
print "$@"
}
### Script : End
Thanks in Advance.


1arryb
User

Nov 20, 2009, 7:19 AM

Post #2 of 5 (3723 views)
Re: [ajitnavasare] problem in error handling. [In reply to] Can't Post

Hi ajitnavasare,

Are you certain that step 1 really "throws an exception?". Any error in cmd1212.exe will happen in a child process forked by the system() call. Even if it dies like a dog Smile it shouldn't cause system() itself to die. To debug, try putting the system and SendKeys() commands each in their own eval{} block. Also, your printout might be more revealing if you label the exception:

Code
eval { 
system(...);
};
if ($@) {
print "system call failed: $@";
exit(-1);
}
...


Cheers,

Larry


vishi
New User

Nov 22, 2009, 3:37 AM

Post #3 of 5 (3710 views)
Re: [1arryb] problem in error handling. [In reply to] Can't Post

Hi,

Avoid using exit(). You can rather use die or croak. So the same code can be changed this way,


Code
use Carp qw(croak); 
eval {
system(...);
};
if ($@) {
croak "system call failed: $@";
}



Thanks,
Vishi.


1arryb
User

Nov 23, 2009, 7:57 AM

Post #4 of 5 (3645 views)
Re: [vishi] problem in error handling. [In reply to] Can't Post

vishi,

You were probably thinking of this:

Quote
Don't use exit to abort a subroutine if there's any chance that someone might want to trap whatever error happened. Use die instead, which can be trapped by an eval.

http://perldoc.perl.org/functions/exit.html

It's good advice, especially if you develop perl modules for general use; however, exit() has a couple of desirable features. 1) You can control (subject to OS/shell vagueries) the exit status of your program; and 2) unlike croak(), die(), etc., exit() doesn't slplat out a message to the console, which can be important if you want to evaluate your program's STDERR output. YMMV.

Update:

I suppose there's also a philosophical question here: Do you prefer to process errors by throwing and catching exceptions or by evaluating the return status of your subroutines? I prefer the latter. Hence, I almost never write a subroutine that intentionally dies() just to return an error.

Cheers,

Larry


(This post was edited by 1arryb on Nov 23, 2009, 8:33 AM)


vishi
New User

Nov 24, 2009, 3:26 AM

Post #5 of 5 (3599 views)
Re: [1arryb] problem in error handling. [In reply to] Can't Post

point well taken.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives