Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception handling

Status
Not open for further replies.

stones1030

Programmer
May 30, 2006
15
US
Is there a way to catch errors in a block of perl code and handle the errors per block.
I have been doing somehting like this
open(...) or die $!;
open (...) or die $!;
print $! unless (some condition);

SO right now I have script which checks for error messages on as many lines as possible. Is there a better way of doing this?If I open 3 files, i have to repeat this or die $! for each of them.
thank you
 
Can you put all that into an eval block then check $@?

[tt]
eval {
open (...);
open (...);
open (...);
}
die $@ if $@;
[/tt]

Not sure if that's correct, just an idea.
 
If you put a die in an eval{} block, you can catch the exception and do whatever you want next. There's a whole section on it in the perlfunc manpage.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top