为什么不在`onComplete`和`onNext`中出现`OnErrorNotImplementedException`?

时间:2018-02-28 17:58:13

标签: java rx-java2

我在学习RxJava2时遇到OnErrorNotImplementedException。我了解到这种异常的发生是由于没有为订阅者实现错误处理程序。所以,我想除了onError方法之外,为什么它不存在。为什么呢?

1 个答案:

答案 0 :(得分:0)

from bokeh.plotting import figure from bokeh.models import ColumnDataSource, CustomJS, CDSView, CustomJSFilter, HoverTool from bokeh.models.widgets import CheckboxButtonGroup from bokeh.io import show, output_notebook from bokeh.palettes import Set1 from bokeh.transform import factor_cmap from bokeh.layouts import widgetbox, layout import pandas as pd import numpy as np import datetime as dt from itertools import cycle, islice output_notebook() N = 24 start_date = dt.date(2016,1,1) nbdays = int(365 / N) df = pd.DataFrame({'Date': [start_date + dt.timedelta(days=i*nbdays) for i in range(1,N+1)], 'Rating': [(100/N)*i for i in range(1,N+1)], 'Plot': list(islice(cycle(range(1, 9)), 0, N)), 'Treatment': list(islice(cycle(range(1, 7)), 0, N)), 'Trial': list(islice(cycle(range(1, 4)), 0, N)), 'Name': list(islice(cycle("ABCDEF"), 0, N)), 'Target': list(islice(cycle("JKLMNOP"), 0, N)), 'Part': list(islice(cycle("WXYZ"), 0, N)) }) columns = 'Plot', 'Trial', 'Name', 'Target', 'Part' unique_items = [df[col].unique() for col in columns] df["Treatment"] = df["Treatment"].astype(str) source = ColumnDataSource(data=df) dummy_source = ColumnDataSource(data={"x":[], "y":[]}) hover = HoverTool(tooltips=[('Date', '@Date{%d/%m/%Y}')] + [(column, '@'+column) for column in columns], formatters={ 'Date': 'datetime', # use 'datetime' formatter for 'Date' field }) p = figure(x_axis_type="datetime", tools=[hover]) color = factor_cmap("Treatment", Set1[9], df.Treatment.unique()) for i, label in enumerate(df.Treatment.unique()): dummy_circle = p.circle(x="x", y="y", source=dummy_source, legend="Treatment {}".format(label), color=Set1[9][i]) dummy_circle.tags = [label] p.legend.location = "bottom_right" p.legend.click_policy = "hide" def source_change(source=source): source.change.emit() callback_source_change = CustomJS.from_py_func(source_change) for item in p.legend[0].items: item.renderers[0].js_on_change("visible", callback_source_change) controls = [CheckboxButtonGroup(labels=items.astype(str).tolist(), active=list(range(len(items)))) for items in unique_items] widgets = widgetbox(*controls) for name, control in zip(columns, controls): control.tags = [name] def func_filter(source=source, legend=p.legend[0], widgets=widgets): window.widgets = widgets visible_treatments = [item.renderers[0].tags for item in legend.items if item.renderers[0].visible] date = source.data['Date'] treatments = source.data['Treatment'] res = [] selectors = {} for widget in widgets.children: col = widget.tags[0] selectors[col] = dict([(widget.labels[i], i) for i in widget.active]) for i in range(len(date)): flag = treatments[i] in visible_treatments for key, val in selectors.items(): if source.data[key][i] not in val: flag = False break res.append(flag) return res view = CDSView(source=source, filters=[CustomJSFilter.from_py_func(func_filter)]) p.circle(x='Date', y='Rating', source=source, view=view, line_color=color, fill_color=color) for control in controls: control.js_on_change("active", callback_source_change) show(layout([[p, widgets]])) 方法不会消耗错误,最终会抛出订阅者。在这种情况下,如果您未处理订阅者,则会抛出doOnError

以下方法将消耗OnErrorNotImplementedException方法中提到的错误。

  

方法1:

subscribe
  

方法2:

Observable.just(new Object())
            .doOnNext(o -> { /* /* This method catches the on next but doesn't consume it. */})
            .doOnComplete(() -> { /* test */})
            .doOnError(throwable -> {/* This method catches the error but doesn't consume it. */})
            .subscribe(o ->
                    {/* success */}
                    ,
                    throwable -> {/* error */} // here error will be consumed at the end
            );

总的来说,你必须消耗错误,否则你将面临同样的错误。