捕获,处理,然后Rethrow例外?(Catch, Handle, then Rethrow Exception?)
我今天遇到了一个有趣的困境。 我有一个处理信息和检查重复值的函数,然后返回下一个不重复的数字。 所以,我有这样的事情:
Public Function GetNextNonDuplicateNumber(NumberToCheck as Long) as Long //the non-duplicate the function will return Dim NonDuplicate as Long If CheckForDuplicate(NumberToCheck) = True Then Throw New DuplicateException() Else NonDuplicate = NumberToCheck End If End Function
然后在函数的底部我有一个catch块,通过递增处理副本,直到我没有重复,如下所示:
Catch ex as DuplicateException NonDuplicate = IncrementToNonDuplicateValue(NumberToCheck) Throw ex Return NonDuplicate End Function
正如你所看到的,我想特别处理异常,但我也想在完成之后抛出它,因为我想提醒函数之外的其他代码。
问题是简单地抛出它会使用
null
值退出函数。 我是在考虑try/catch
错误的方式,还是有办法解决这个问题?I ran into an interesting dilemma today. I have a function that handles information and checks for duplicate values, then returns the next number that is not a duplicate. So, I have something like this:
Public Function GetNextNonDuplicateNumber(NumberToCheck as Long) as Long //the non-duplicate the function will return Dim NonDuplicate as Long If CheckForDuplicate(NumberToCheck) = True Then Throw New DuplicateException() Else NonDuplicate = NumberToCheck End If End Function
Then at the bottom of the function I have a catch block that handles the duplicate by incrementing until I don't have a duplicate any more, like this:
Catch ex as DuplicateException NonDuplicate = IncrementToNonDuplicateValue(NumberToCheck) Throw ex Return NonDuplicate End Function
As you can see, I want to handle the exception specifically, but I also want to throw it when I'm done because I want to alert other code outside the function.
The problem is that simply throwing it exits out of the function with a
null
value. Am I thinking about atry/catch
the wrong way, or is there a way around this?
原文:https://stackoverflow.com/questions/1050860
最满意答案
试试这个
<?php $row = 1; if (($handle = fopen("abc.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $username = $data[0]; $usernames = explode(' ', $email); print($usernames[1]).'<br />'; } fclose($handle); } ?>
try this one
<?php $row = 1; if (($handle = fopen("abc.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $username = $data[0]; $usernames = explode(' ', $email); print($usernames[1]).'<br />'; } fclose($handle); } ?>
相关问答
更多-
假设您已经将csv文件解析为数组 。 array_keys函数的第二个参数是$search_value 如果指定,则仅返回包含这些值的键。 要搜索特定列,请使用array_column : $res = array_keys(array_column($csv, 1), "yes"); 参见eval.in的测试 ; 这将返回第二列中所有“是”匹配的键。 Assuming you have already parsed the csv file into an array. The second param ...
-
如何将csv文件的列中的所有值更改为特定值php(How to change all values in a column of a csv file to a specific value php)[2023-06-22]
您可以使用preg_replace并立即替换所有值,而不是循环CSV文件的每一行。 只需两行代码即可。 $csv = file_get_contents($path); file_put_contents($path, preg_replace("/(.*),\d+/", "$1,500", $csv)); 其中$ path是路径和CSV文件。 你可以在这里看到它: https : //3v4l.org/Mc3Pm You can use preg_replace and replace all valu ... -
PHP使用fputcsv CSV在每列的列中写入相同的数据(PHP use the fputcsv CSV to write same data in column for each row)[2022-12-13]
如果要将数组的键显示为第一列,则可以执行此操作。 如果您没有关联数组,或者您希望硬编码列标题,那么您可以简单地更改硬编码数组的array_keys($line) 。 $handle = fopen('exemple.csv', 'w'); $keys = false; foreach ($newCsvData as $line) { if ($keys === false) { //$header = array('HeaderOne', 'HeaderTwo', 'HeaderThr ... -
PHP - fgetcsv循环用于逐列从csv文件中获取数据(PHP - fgetcsv loop for get data from csv file column by column)[2022-10-06]
在你的代码中: for ($col=1; $col<=32; $col++) { while ($data = fgetcsv($read, max, ";")) { 您正试图从文件中提取32次所有行。 第一次迭代获取文件中的所有行,因为while循环在fgetcsv返回false (当没有更多数据要读取时)时结束。 我需要使用fgetcsv (或者其他解决方案)来传递csv并逐列获取数据。 您不能逐列读取数据,因为文件是逐行读取的,而这些行代表了表格行。 如果要按列迭代表数据,可以将整个表读入数组, ... -
试试这个 '; } f ...
-
那是因为你告诉PHP continue (跳过fputcsv()代码行)如果该行包含Laravel,那么它根本就不会写任何一行 $table = fopen('tech.csv','r'); $temp_table = fopen('table_temp.csv','w'); $names = ['Laravel', 'Codeigniter']; // The values that you're looking to remove ...
-
谢谢你的帮助,最终得到了它。 function getColumn($csv, $column){ $header = array_shift($csv); $col = array_search($column, $header); foreach ($csv as $row) { $array[] = $row[$col]; } return $array; } thanks for all your help, eventual ...
-
如何获取CSV中列中不同值的总数?(How do I get the total number of distinct values in a column in a CSV?)[2022-11-12]
使用awk你可以做到: awk -F, '!seen[$1]++{c++} END{print c}' file 2 这个awk命令使用键$1 ,并将它们存储在一个看到的数组中。 当第一次填充密钥时,其值增加到1。 每当我们得到一个唯一的密钥时,我们增加计数c并最终打印出来。 Using awk you can do: awk -F, '!seen[$1]++{c++} END{print c}' file 2 This awk command uses key $1, and stores the ... -
在PHP中组合csv列值(Combine csv column values in PHP)[2023-08-03]
为您的解决方案使用以下代码 $file = ('my.csv'); $fh = fopen($file, 'rb'); $tag = array(); $qty = array(); $row=1; while($col = fgetcsv($fh)) { if($row == 1){ $row++; continue; } //skip 1st row $num = count($fh); if (isset($tag[$col[3]])) { //$tag[$col[3]]++; $tag[$col[3 ... -
如何将第1列中的值与csv和parse转换为csv PHP(How to sum values from column 1 from csv and parse to csv PHP)[2022-01-15]
试试这个: $newData = array(); foreach ($tsvFile as $line => $row) { if ($line > 0) { if (isset($newData[$row[0]])) { $newData[$row[0]]+= $row[1]; } else { $newData[$row[0]] = $row[1]; } } } foreach ($newD ...