首页 \ 问答 \ doOnError不会捕获异常(doOnError does not catch the exception)

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
更新时间:2023-11-09 15:11

最满意答案

您可以逐行扫描文件的内容并使用以下代码进行处理:

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());

相关问答

更多
  • 为什么这不会引发例外? 您可能没有启用范围检查 。 在{$R+}状态中,所有数组和字符串索引表达式都被验证为在定义的范围内,并且所有对标量和子范围变量的赋值都被检查在范围内。 **如果范围检查失败,则引发ERangeError异常(或者如果未启用异常处理,程序将终止)。 范围检查默认为OFF。 要启用它,您可以将此指令添加到您的代码中: {$RANGECHECKS ON} 或者,您可以在“Delphi编译器|编译|运行时错误”下的“项目选项”中启用它。 Why is this not raising th ...
  • 而不是使用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], ...
  • 当你遍历它们时,你正在改变你的列表(通过在循环中调用.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 [] ...
  • 如果我不得不猜测我会说这可能是这样的: Path.Split('/')[3] 如果你在http:// localhost / myapp下运行,那么在调用Path.Split之后,如果你在http://www.myapp.com下运行,你将在数组中拥有更多元素。 有可能你在生产中只有3个元素,而不是你可能在dev中的4个元素。 编辑: 对于您发布的页面,对Request.Path的调用将返回: "/backend/default.aspx" 当你对'/'进行拆分时,你只会得到3个元素: [0] = "" ...
  • 你正在重用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 ...
  • 在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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。