快速错误处理:范围(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
最满意答案
问题是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 matrixy
, it takes the underlying data fromx
(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"
withclass(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 usingas.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"
相关问答
更多-
PHPExcel:以字符串形式读取所有值(日期,时间,数字)(PHPExcel: Read all values (date, time, numbers) as strings)[2022-03-14]
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 ...
-
用字符串计数(数字)(Counting with strings (numbers))[2019-12-03]
这不是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
-
JavaScript函数将日期字符串参数更改为一系列数字(JavaScript function changes date string parameter to a series of numbers)[2023-01-11]
只要通过这样的日期,它应该工作。 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 ... -
避免魔法字符串和数字(Avoiding magic strings and numbers)[2021-12-20]
给每个菜单项在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 ...
-
从数字创建字符串(Create strings from numbers)[2022-04-15]
来自范例的?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" ...