未连接到Internet时发生致命exception

我正在努力学习Kotlinrxjava 。 我有一个非常简单的应用程序,只需点击一个button ,即可从网站获取一些数据。

一切工作正常,除非我关闭我的手机数据,在这种情况下,我得到一个致命的例外,没有解释为什么。

我的代码:

 // In the activity's onCreate ViewObservable.clicks(button) .observeOn(Schedulers.io()) .subscribe{ fetchTest().observeOn(AndroidSchedulers.mainThread()) .subscribe{ s:String -> textView.text = s } } // Elsewhere in the activity code @Throws(IOException::class) private fun fetchTest(): Observable { val url = URL(MY_URL) val httpConnection = url.openConnection() var stream = httpConnection.inputStream val reader = BufferedReader(InputStreamReader(stream)) val response = reader.readLines().reduce { x, y -> x + y } stream?.close() return Observable.just(response) } 

当我处于调试模式时,错误发生在这里:

 var stream = httpConnection.inputStream 

而logcat完全没有帮助:

 02-12 10:13:37.748 8616-8645/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: RxCachedThreadScheduler-1 Process: com.example.app, PID: 8616 

我知道错误是因为没有网络连接,但添加try / catch无法捕捉它,所以我不知道该怎么办

观察者的用户也需要实现onError