doOnError不会捕获异常(doOnError does not catch the exception)
我刚刚开始使用RxJava,但也许还没有点击。
1。
Integer[] items = {1, 2, 3, 0, 0, 4, 5, 6, 1}; Observable.from(items) .map(this::invert) .subscribe(i -> Log.d(LOG_TAG, "Inverted: " + i), t -> Log.d(LOG_TAG, "Error: " + t.getMessage()));
2。
Integer[] items = {1, 2, 3, 0, 0, 4, 5, 6, 1}; Observable.from(items) .map(this::invert) .doOnError(t -> Log.d(LOG_TAG, "Error: " + t.getMessage())) .doOnNext(i -> Log.d(LOG_TAG, "Inverted: " + i)) .subscribe();
invert
功能:int invert(int i) { return 1 / i; }
第一个正常执行,当抛出异常时,执行
onError
。 但另一方面,第二个不起作用,所以异常一直抛到调用方法。两个代码块之间有什么区别?
I am just getting started with RxJava, but maybe something did not click yet.
1.
Integer[] items = {1, 2, 3, 0, 0, 4, 5, 6, 1}; Observable.from(items) .map(this::invert) .subscribe(i -> Log.d(LOG_TAG, "Inverted: " + i), t -> Log.d(LOG_TAG, "Error: " + t.getMessage()));
2.
Integer[] items = {1, 2, 3, 0, 0, 4, 5, 6, 1}; Observable.from(items) .map(this::invert) .doOnError(t -> Log.d(LOG_TAG, "Error: " + t.getMessage())) .doOnNext(i -> Log.d(LOG_TAG, "Inverted: " + i)) .subscribe();
The
invert
function:int invert(int i) { return 1 / i; }
The first executes normally, and when the exception is thrown the
onError
is executed. But on the other hand, the second does not work, so the exception is thrown all the way up to the calling method.What is the difference between the two blocks of code?
原文:https://stackoverflow.com/questions/41508408
最满意答案
您可以逐行扫描文件的内容并使用以下代码进行处理:
public class MyProj03 { public static void main(String[] args) throws IOException { File file = new File("p3text.txt"); try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); Person one = new Person(); one.parseCommaDelim(line); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
我还建议你改变片段代码:
Person instance = new Person(); instance.name = tokens[0]; instance.age = Integer.parseInt(tokens[1]); //ArrayIndexOutOfBoundsException error instance.city = tokens[2]; instance.sibCount = Integer.parseInt(tokens[3]);
对此:
Person instance = new Person(); instance.name = tokens[0]; instance.age = Integer.parseInt(tokens[1].trim()); //ArrayIndexOutOfBoundsException error instance.city = tokens[2]; instance.sibCount = Integer.parseInt(tokens[3].trim());
You can scan file's content line by line and process with this code:
public class MyProj03 { public static void main(String[] args) throws IOException { File file = new File("p3text.txt"); try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); Person one = new Person(); one.parseCommaDelim(line); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
also I recommend you to change the fragment codes :
Person instance = new Person(); instance.name = tokens[0]; instance.age = Integer.parseInt(tokens[1]); //ArrayIndexOutOfBoundsException error instance.city = tokens[2]; instance.sibCount = Integer.parseInt(tokens[3]);
to this:
Person instance = new Person(); instance.name = tokens[0]; instance.age = Integer.parseInt(tokens[1].trim()); //ArrayIndexOutOfBoundsException error instance.city = tokens[2]; instance.sibCount = Integer.parseInt(tokens[3].trim());
相关问答
更多-
Delphi数组索引边界(Delphi array index bounds)[2023-08-30]
为什么这不会引发例外? 您可能没有启用范围检查 。 在{$R+}状态中,所有数组和字符串索引表达式都被验证为在定义的范围内,并且所有对标量和子范围变量的赋值都被检查在范围内。 **如果范围检查失败,则引发ERangeError异常(或者如果未启用异常处理,程序将终止)。 范围检查默认为OFF。 要启用它,您可以将此指令添加到您的代码中: {$RANGECHECKS ON} 或者,您可以在“Delphi编译器|编译|运行时错误”下的“项目选项”中启用它。 Why is this not raising th ... -
'index超出了数组'异常处理错误的范围('index was outside the bounds of the array' exception handling error)[2023-08-03]
而不是使用try-catch块来处理错误,您应该在尝试访问它之前检查是否找到了该进程: private void timer1_Tick(object sender, EventArgs e) { #region BaseAddress Process[] test = Process.GetProcessesByName("process"); if (test.Any()) { // Process is running. int Base ... -
你在循环中使用了lines[i + 3] ,但你的检查只能确保i + 2在范围内 - 而且你在循环中使用4个值而不是4使得它看起来应该可能是: for (var i = 12; i + 3 < lines.Length; i += 4) { Items.Add(new ItemProperties { Item = lines[i], Description = lines[i + 1], Quantity = lines[i + 2], ...
-
数组索引超出范围(Array index out of bounds)[2022-04-07]
当你遍历它们时,你正在改变你的列表(通过在循环中调用.remove() )。 您正在检查是否与其发生碰撞时删除子弹。 一种解决方案是给外星人/子弹/其他这样的对象一个isDead变量。 然后,不要在循环内删除它们,等到循环之后并删除isDead == true所有人。 You're altering your lists as you're traversing them (by calling .remove() inside the loop). You're deleting the bullet i ... -
将listsize定义为static ,它将获得默认值0 。 然后你调用createArithmeticSeq(int [] list) ,你基本上传递一个带有0个元素空间的列表。 如果在创建int [] list = new int [listSize];之后更改了listsize的值int [] list = new int [listSize]; ,列表的大小不会改变。 因此,您必须在知道它的大小后创建数组。 为此,请将主方法修改为 public static void main (String [] ...
-
索引超出数组的范围(Index outside the bounds of the array)[2022-03-03]
如果我不得不猜测我会说这可能是这样的: Path.Split('/')[3] 如果你在http:// localhost / myapp下运行,那么在调用Path.Split之后,如果你在http://www.myapp.com下运行,你将在数组中拥有更多元素。 有可能你在生产中只有3个元素,而不是你可能在dev中的4个元素。 编辑: 对于您发布的页面,对Request.Path的调用将返回: "/backend/default.aspx" 当你对'/'进行拆分时,你只会得到3个元素: [0] = "" ... -
指数超出范围(Index out of bounds)[2023-08-16]
你正在重用x,它正在破坏它:lambda F将x作为参数,但也已经使用了你的x数组。 尝试F = lambda z: y0[0] - z + h*rhs(0.5*(z+y0[0]),y0[1],x[i]) 。 You are reusing x, which is breaking it: the lambda F takes x as an argument, but also already uses your x array. Try F = lambda z: y0[0] - z + h*rhs(0 ... -
看看这个链接。 以下是一些摘录: 此错误的常见原因是指向错误方向的关联。 (如果手动编辑模型,非常容易做到的事情,部分原因是关联箭头指针位于ER图中出现的另一端) 和 我遇到了同样的问题,这是因为我的主键是两列而不是传统的。 (两个指导)。 当我添加第三列是唯一的主键列时,它起作用了。 更新:做了一些更多的探索,发现这个 SO帖子。 看起来它可能与您的DBML有关... Take a look at this link. Here are a few excerpts: A common cause of ...
-
您可以逐行扫描文件的内容并使用以下代码进行处理: public class MyProj03 { public static void main(String[] args) throws IOException { File file = new File("p3text.txt"); try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scan ...
-
数组索引超出范围?(Array index out of bounds? How?)[2022-07-26]
在next()你一直在递增row但是没有故障安全索引超出row检查row因为有col ,所以不能保证row超过大于8的值,即9。 因此,确保在行resi(matrica2,row + 1, 0); )中递增( row+1 )之前检查row是否小于8 resi(matrica2,row + 1, 0); 。 private static void next(int [][] matrica2,int row, int col) { if (col 8) { resi(matrica2,row, co ...