示例代码中,抛出了个自定义异常
throw BOOMException(tokenBean.msg ?: BOOM_EXCEPTION_DEFAULT) // BOOM_EXCEPTION_DEFAULT 是常量,代表默认的异常信息。
但是报错了,try catch 没有拦截住。
因为 BOOMException 继承了 Throwable。
改为 继承 Exception ,可以正常拦截了。
原因:Exception 是 Throwable 的子类,catch 中拦截的是 Exception 不是 Throwable。
示例代码:
/*** 激活流程*/fun token() {GlobalScope.launch(Dispatchers.Main) {try {//获取URLSval url = RetrofitManager.getApiService().urls()if (url?.code == 200) {RoomConstants.saveUrl(url.data)//同步服务器时间url.data?.ts?.let {DeviceControlFactory.createDeviceControl(context).setTIme(it)}val openSdkConfig = BoomMeeting.getOpenSdkConfig()val busSdkConfig = BoomMeeting.getBusSdkConfig()val urls = url.dataopenSdkConfig.appId = urls?.saasApiAppIdopenSdkConfig.serverUrl = urls?.saas_api?.let { StringUtils.isHasSlash(it) }openSdkConfig.collectionUrl = urls?.collectionLog.d("urls?.saasApiAppId", urls?.saas_api?.let { StringUtils.isHasSlash(it) })BoomMeeting.reInit(BoomApp.getContext(), openSdkConfig, busSdkConfig)} else {// urls 访问失败throw BOOMException(url?.msg ?: BOOM_EXCEPTION_DEFAULT)}//获取publickeyval article = RetrofitManager.getApiService().public_key()//获取硬件信息认证val check = checkRequestBody(article)?.let {RetrofitManager.getApiService().check(it)}//获取Token,激活成功val tokenBean = tokeneQuestBody(check)?.let {RetrofitManager.getApiService().token(it)} ?: throw BOOMException(check?.msg ?: BOOM_EXCEPTION_DEFAULT)//拿到Token保存if (tokenBean.code == 200) {SPUtil.setString(contexts,SPUtil.TAG_TOKEN,SPUtil.KEY_TOKEN,tokenBean.data?.token)} else {// 获取 token 访问失败throw BOOMException(tokenBean.msg ?: BOOM_EXCEPTION_DEFAULT)}val deviceInfo = RetrofitManager.getApiService().deviceInfoActiva()if (deviceInfo?.code == 200) {RoomConstants.saveDevicesInfo(deviceInfo.data)//初始化MqttMqttClient.getInstence().uninit()MqttClient.getInstence().init()listener?.activationSuccess()} else {netWork(REACTIVATE,deviceInfo?.msg?: context?.getString(R.string.activating_warning_reactivate)!!)}} catch (e: ConnectException) {netWork(NO_NETWORK)} catch (e: IllegalArgumentException) {netWork(NO_NETWORK)} catch (e: UnknownHostException) {netWork(NO_NETWORK)} catch (e: Exception) {//请求异常NBMLogCat.info("$TAG------>>>>请求异常:$e.message--->>>")if (e.message?.contains("connect timed") == true) {netWork(NO_NETWORK)} else {if (isConnect) {netWork(REACTIVATE,e.message?: context?.getString(R.string.activating_warning_reactivate)!!)} else {netWork(NO_NETWORK)}}}}}
