首页 \ 问答 \ 捕获,处理,然后Rethrow例外?(Catch, Handle, then Rethrow Exception?)

捕获,处理,然后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 a try/catch the wrong way, or is there a way around this?


原文:https://stackoverflow.com/questions/1050860
更新时间:2022-09-09 08:09

最满意答案

试试这个

<?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);
}
?>

相关问答

更多

相关文章

更多

最新问答

更多
  • 以编程方式创建视频?(Create videos programmatically?)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • javascript数组,如何改变这个数组结构(javascript arrays, how to change this array structure)
  • 在ASP.NET Web API中使用多个Get方法进行路由(Routing with multiple Get methods in ASP.NET Web API)
  • 用于backbone.js验证的自定义验证器(Custom validator for backbone.js validation)
  • const char *与其他指针有什么不同?(Is const char * different from other pointers? [duplicate])
  • 无效的列索引,使用PreparedStatement更新(Invalid column index , update using PreparedStatement)
  • watchOS WCSession'已配对'和'watchAppAvailable'不可用(watchOS WCSession 'paired' and 'watchAppAvailable' are unavailable)
  • CalledFromWrongThreadException在Android上执行JUnit测试(CalledFromWrongThreadException exercising JUnit tests on Android)
  • 如何把文件保存到你的应用程序目录中?(How to put\save files into your application directory? (adobe air))
  • 美元符号在Java方法描述符中的含义?(Meanings of dollar sign in Java method descriptor?)
  • font-size的含义是什么:1em / 2em?(What doe the meaning of font-size:1em/2em?)
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • Android - 检测与特定wifi ssid断开连接的正确方法?(Android - Correct way to detect disconnecting from a particular wifi ssid?)
  • 通过Shell脚本将文件转换为另一个文件(Convert File To Another File By Shell Script)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • 如何过滤magento废弃的购物车报告集合(How to Filter the magento abandoned cart report collection)
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • web api http post传递对象504接收失败(web api http post passing object 504 Receive Failure)
  • Rails从视图编辑模型上的多个属性的方法(Rails way to edit multiple attributes on a model from a view)
  • 总是用{}初始化对象是否是好习惯?(Is it good habit to always initialize objects with {}?)
  • 在方案中编写特殊字符到输出端口(编译器设计)(writing special characters to output port in scheme (compiler design))
  • 电脑等级考试得证有多大用处?
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • 第一次调用函数将无法按预期工作,但下一次工作正常(calling a function on the first time won't work as expected, but next time is working)
  • 如何优化使用BigInteger操作执行时间的代码(How to optimize the code that uses BigInteger operations for execution time)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何提供个人资料信息,以便Passport.js可以使用它?(how does Profile information should be provided so Passport.js can use it?)
  • 有没有办法初始化jquery数据表中的细节?(is there any way to initialize details in jquery datatable?)