【发布时间】:2014-09-22 19:40:36
【问题描述】:
我有一个使用 PHP 中的 PDFlib 库的脚本,我需要遍历一堆块并告诉我它们的名称和描述(如果存在描述)。当$pdf->pcos_get_string() 调用失败时,PDFlib 总是会抛出异常,所以我需要捕获它。
问题是我需要在抛出特定执行后继续执行我的循环。这似乎不是什么大问题,但我的循环在抛出并捕获异常后停止执行。不是每个块都有描述,所以我不能只是假设它们有。
代码如下:
try{
$num_blocks = $pdf->pcos_get_number($input_file,'length:pages[0]/blocks');
for($i=0;$i<$num_blocks;$i++){
# This works fine
$block_name = $pdf->pcos_get_string($input_file,'pages[0]/blocks['.$i.']/Name');
try{
# This will always throw an exception when there is no description
$block_desc = $pdf->pcos_get_string($input_file,'pages[0]/blocks['.$i.']/Description');
}catch(PDFlibException $e){
# I want to print this, then go to the next loop iteration
print 'Found block '.$block_name.' with no description'.PHP_EOL;
continue;
}catch(Exception $e){
print 'Some other exception occurred'.PHP_EOL;
}
if($block_desc)
# Print the block name
print 'Found block '.$block_name.' with description '.$block_desc.PHP_EOL;
}
}catch(PDFlibException $e){
print $e;
exit(1);
}catch(Exception $e){
print $e;
exit(1);
}
我希望输出如下:
找到带有描述您居住地的区块地址
找到块名称和描述你的名字
找到没有描述的块状态
找到带有描述您的邮政编码的块 Zip
...(这十六行)
我的输出实际上是这样的:
找到带有描述您居住地的区块地址
找到块名称和描述你的名字
找到没有描述的块状态
我真正的问题是,一旦脚本完成异常处理程序,它就会在我的循环中间停止执行。我需要做什么来确保循环继续执行?
提前感谢您的帮助。
【问题讨论】:
标签: php exception-handling pdflib