首页 \ 问答 \ 快速错误处理:范围(swift errorhandling: scope)

快速错误处理:范围(swift errorhandling: scope)

在swift 2中,作用域的错误处理应该有多大? 我理解错误处理(我认为),但我也怀疑实现细节。 如果我有一个调用抛出函数的函数,我应该如何处理我的do-try-catch的范围?

我应该这样做:

func doLotsOfErrorProneWork()
{
  //everything in scope
 do {
    try f1()
    f2()
    f3()
    try f4()
    f5()
 } catch {}
}

或这个:

func doLotsOfErrorProneWork()
{
  //smallest scope possible
  do {
    try f1()
  } catch {}

    f2()
    f3()

  do {
    try f4()
  } catch {}

  f5()
}

这有什么不同吗? 有什么不同? 一个区别可能是f1和f4之间抛出的错误差异。 但是假设只有一个投掷功能。

func doLotsOfErrorProneWork()
{
  //everything in scope
  do {
    try f1()
    f2()
  } catch {}

}

或这个

func doLotsOfErrorProneWork()
{
  //smallest scope possible
  do {
    try f1()
  } catch {}
    f2()
}

或者只是风格问题?


How large should the scope be for error handling in swift 2? I understand error handling (I think), but I am also doubting on the implementation details. If I have a function that calls throwing functions, how should I handle the scope of my do-try-catch?

Should I do this:

func doLotsOfErrorProneWork()
{
  //everything in scope
 do {
    try f1()
    f2()
    f3()
    try f4()
    f5()
 } catch {}
}

or this:

func doLotsOfErrorProneWork()
{
  //smallest scope possible
  do {
    try f1()
  } catch {}

    f2()
    f3()

  do {
    try f4()
  } catch {}

  f5()
}

Does it make a difference? What is the difference? One difference is probably the difference in error that is thrown between f1 and f4. But suppose there is only one throwing function.

func doLotsOfErrorProneWork()
{
  //everything in scope
  do {
    try f1()
    f2()
  } catch {}

}

or this

func doLotsOfErrorProneWork()
{
  //smallest scope possible
  do {
    try f1()
  } catch {}
    f2()
}

Or is it just a matter of style?


原文:https://stackoverflow.com/questions/35502504
更新时间:2023-10-31 14:10

最满意答案

问题是R不允许矩阵填充日期。 "matrix""Date"都是类。 R中Date对象的底层表示只是一个整数。 因此,当您创建矩阵y ,它会从x (它是一个整数数组)中获取基础数据,添加一个维度属性,并使类为"matrix"

这个问题没有任何干净的解决方法。 尽管如此,你可以使用一些黑客技巧。 例如,你可以显式强制y是类"Date"以及class(y)<-c("matrix","Date")y仍然会打印出日期向量,但您可以使用矩阵坐标来操作它:

> class(y)<-c("matrix","Date")
> head(y)
 [1] "2013-09-02" "2013-09-09" "2013-09-16" "2013-09-23" "2013-09-30"
 [6] "2013-10-07" "2013-09-03" "2013-09-10" "2013-09-17" "2013-09-24"
[11] "2013-10-01" "2013-10-08" "2013-09-04" "2013-09-11" "2013-09-18"
[16] "2013-09-25" "2013-10-02" "2013-10-09" "2013-09-05" "2013-09-12"
[21] "2013-09-19" "2013-09-26" "2013-10-03" "2013-10-10" "2013-09-06"
[26] "2013-09-13" "2013-09-20" "2013-09-27" "2013-10-04" "2013-10-11"
[31] "2013-09-07" "2013-09-14" "2013-09-21" "2013-09-28" "2013-10-05"
[36] "2013-10-12" "2013-09-08" "2013-09-15" "2013-09-22" "2013-09-29"
[41] "2013-10-06" "2013-10-13"
> y[1,]
[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[6] "2013-09-07" "2013-09-08"
> y[1,]<-y[1,]+1
> y[1,]
[1] "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" "2013-09-07"
[6] "2013-09-08" "2013-09-09"

您也可以使用数据框而不是矩阵:

> y<-data.frame(y)
> y<-data.frame(lapply(y,function(x) {class(x)<-"Date";x}))
> head(y)
          X1         X2         X3         X4         X5         X6         X7
1 2013-09-02 2013-09-03 2013-09-04 2013-09-05 2013-09-06 2013-09-07 2013-09-08
2 2013-09-09 2013-09-10 2013-09-11 2013-09-12 2013-09-13 2013-09-14 2013-09-15
3 2013-09-16 2013-09-17 2013-09-18 2013-09-19 2013-09-20 2013-09-21 2013-09-22
4 2013-09-23 2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-28 2013-09-29
5 2013-09-30 2013-10-01 2013-10-02 2013-10-03 2013-10-04 2013-10-05 2013-10-06
6 2013-10-07 2013-10-08 2013-10-09 2013-10-10 2013-10-11 2013-10-12 2013-10-13

第三种可能性是以字符格式保存y中的元素,然后在需要时使用as.Date将它们转换为日期:

> y<-matrix(as.character(x),nrow=20,byrow=T)
> head(y)
     [,1]         [,2]         [,3]         [,4]         [,5]        
[1,] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[2,] "2013-09-09" "2013-09-10" "2013-09-11" "2013-09-12" "2013-09-13"
[3,] "2013-09-16" "2013-09-17" "2013-09-18" "2013-09-19" "2013-09-20"
[4,] "2013-09-23" "2013-09-24" "2013-09-25" "2013-09-26" "2013-09-27"
[5,] "2013-09-30" "2013-10-01" "2013-10-02" "2013-10-03" "2013-10-04"
[6,] "2013-10-07" "2013-10-08" "2013-10-09" "2013-10-10" "2013-10-11"
     [,6]         [,7]        
[1,] "2013-09-07" "2013-09-08"
[2,] "2013-09-14" "2013-09-15"
[3,] "2013-09-21" "2013-09-22"
[4,] "2013-09-28" "2013-09-29"
[5,] "2013-10-05" "2013-10-06"
[6,] "2013-10-12" "2013-10-13"
> as.Date(y[1,])
[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[6] "2013-09-07" "2013-09-08"

The problem is R doesn't allow matrices to be populated with dates. Both "matrix" and "Date" are classes. The underlying representation of a Date object in R is just an integer. So when you create your matrix y, it takes the underlying data from x (which is an array of integers), adds a dimension attribute, and makes the class "matrix".

There's not any clean way around this problem. There are a few hacks you can use, though. For example, you could explicitly force y to be of class "Date" as well as "matrix" with class(y)<-c("matrix","Date"). y would still print out as just a vector of dates, but you would be able to manipulate it using matrix cordinates:

> class(y)<-c("matrix","Date")
> head(y)
 [1] "2013-09-02" "2013-09-09" "2013-09-16" "2013-09-23" "2013-09-30"
 [6] "2013-10-07" "2013-09-03" "2013-09-10" "2013-09-17" "2013-09-24"
[11] "2013-10-01" "2013-10-08" "2013-09-04" "2013-09-11" "2013-09-18"
[16] "2013-09-25" "2013-10-02" "2013-10-09" "2013-09-05" "2013-09-12"
[21] "2013-09-19" "2013-09-26" "2013-10-03" "2013-10-10" "2013-09-06"
[26] "2013-09-13" "2013-09-20" "2013-09-27" "2013-10-04" "2013-10-11"
[31] "2013-09-07" "2013-09-14" "2013-09-21" "2013-09-28" "2013-10-05"
[36] "2013-10-12" "2013-09-08" "2013-09-15" "2013-09-22" "2013-09-29"
[41] "2013-10-06" "2013-10-13"
> y[1,]
[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[6] "2013-09-07" "2013-09-08"
> y[1,]<-y[1,]+1
> y[1,]
[1] "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" "2013-09-07"
[6] "2013-09-08" "2013-09-09"

You could also use a data frame instead of a matrix:

> y<-data.frame(y)
> y<-data.frame(lapply(y,function(x) {class(x)<-"Date";x}))
> head(y)
          X1         X2         X3         X4         X5         X6         X7
1 2013-09-02 2013-09-03 2013-09-04 2013-09-05 2013-09-06 2013-09-07 2013-09-08
2 2013-09-09 2013-09-10 2013-09-11 2013-09-12 2013-09-13 2013-09-14 2013-09-15
3 2013-09-16 2013-09-17 2013-09-18 2013-09-19 2013-09-20 2013-09-21 2013-09-22
4 2013-09-23 2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-28 2013-09-29
5 2013-09-30 2013-10-01 2013-10-02 2013-10-03 2013-10-04 2013-10-05 2013-10-06
6 2013-10-07 2013-10-08 2013-10-09 2013-10-10 2013-10-11 2013-10-12 2013-10-13

A third possiblity is to keep the elements in y in character format, and then convert them to dates using as.Date when you need them:

> y<-matrix(as.character(x),nrow=20,byrow=T)
> head(y)
     [,1]         [,2]         [,3]         [,4]         [,5]        
[1,] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[2,] "2013-09-09" "2013-09-10" "2013-09-11" "2013-09-12" "2013-09-13"
[3,] "2013-09-16" "2013-09-17" "2013-09-18" "2013-09-19" "2013-09-20"
[4,] "2013-09-23" "2013-09-24" "2013-09-25" "2013-09-26" "2013-09-27"
[5,] "2013-09-30" "2013-10-01" "2013-10-02" "2013-10-03" "2013-10-04"
[6,] "2013-10-07" "2013-10-08" "2013-10-09" "2013-10-10" "2013-10-11"
     [,6]         [,7]        
[1,] "2013-09-07" "2013-09-08"
[2,] "2013-09-14" "2013-09-15"
[3,] "2013-09-21" "2013-09-22"
[4,] "2013-09-28" "2013-09-29"
[5,] "2013-10-05" "2013-10-06"
[6,] "2013-10-12" "2013-10-13"
> as.Date(y[1,])
[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06"
[6] "2013-09-07" "2013-09-08"

相关问答

更多
  • toArray()支持以下参数: /** * @param mixed $nullValue Value returned in the array entry if a cell doesn't * exist * @param boolean $calculateFormulas Should formulas be calculated? * @param boolean $form ...
  • 我强烈建议您不要自己编写任何解析器和编写器,而是依赖现有的库和标准。 当您想要写入/读取XML文件时,我强烈建议使用该标准作为日期时间: ISO 8601 。 Java的: 你可以使用Joda Time ,一个支持ISO 8601的库。格式weekDateTimeNoMillis (参见doc )提供了你想要保留最终存储时间的可能性所需的一切: 日期 时间 时区(UTC的'Z'或不同时区的+/- HH:MM)。 如果您不想存储时间信息,则date (请参阅doc )可能是正确的。 它的格式为yyyy-MM- ...
  • 使用DateTime API最初解析您的字符串 $dt = DateTime::createFromFormat('d-m-Y H:i', $fecha.' '.$hora); $newDt = $dt->format('Y-m-d H:i'); 演示~ https://eval.in/827662 Use the DateTime API to parse your string(s) initially $dt = DateTime::createFromFormat('d-m-Y H:i', $fe ...
  • 问题是R不允许矩阵填充日期。 "matrix"和"Date"都是类。 R中Date对象的底层表示只是一个整数。 因此,当您创建矩阵y ,它会从x (它是一个整数数组)中获取基础数据,添加一个维度属性,并使类为"matrix" 。 这个问题没有任何干净的解决方法。 尽管如此,你可以使用一些黑客技巧。 例如,你可以显式强制y是类"Date"以及class(y)<-c("matrix","Date") 。 y仍然会打印出日期向量,但您可以使用矩阵坐标来操作它: > class(y)<-c("matrix","Da ...
  • 这不是Android问题,这是Java问题。 try { String seven = Integer.toString(Integer.parseInt(five) + Integer.parseInt(two)); } catch (NumberFormatException nfex) { // one of the strings was not a number } This is not an Android question, this is Java question. try { ...
  • 您可以找到有用的链接 试试这个 sql小提琴! 或试试这个: SELECT min(CONVERT(price,UNSIGNED INTEGER)) AS num FROM sample You can find the link helpful Try this sql fiddle! or try this: SELECT min(CONVERT(price,UNSIGNED INTEGER)) AS num FROM sample
  • 只要通过这样的日期,它应该工作。 function LoadDatatable(to, from) { alert(to); alert(from); } LoadDatatable('@Model.dateFrom', '@Model.dateTo'); 请在这里查看更多。 Just pass the date like this, It should work. function LoadDatatable(to, from) { alert(to); alert(fr ...
  • 给每个菜单项在WinForms设计器中的名称(我假设),然后通过该名称引用它。 然后在你的代码中使用它: menuExit.Visible = false; 如果菜单项以编程方式添加,请执行以下操作: class MyForm : Form { private MenuItem menuExit; ... myMenu.Items.Add(menuExit = new MenuItem(...)); ... } 然后仍然通过menuExit名称访问它。 避免 ...
  • 您可以使用parseFloat或parseInt方法,也可以看看尝试将data- attribute的内容转换为适当数据类型的jQuery 数据方法 id = $('a').data('id') console.log id == 5 You may use parseFloat or parseInt methods, and also take a look at jQuery data method which tries to convert content of data- attribute ...
  • 来自范例的?ordinal_format library(scales) ordinal_format()(1:10) # [1] "1st" "2nd" "3rd" "4th" "5th" "6th" "7th" "8th" "9th" "10th" From example ?ordinal_format in scales library(scales) ordinal_format()(1:10) # [1] "1st" "2nd" "3rd" "4th" "5th" ...

相关文章

更多

最新问答

更多
  • 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)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)