HarmonyOS 鸿蒙Next try catch是异步执行么?
HarmonyOS 鸿蒙Next try catch是异步执行么?
Failed to get currentTime. message: ${e.message}, code: ${e.code}
); } //获取版本号 let versionCode = ‘’ // 更改 let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA; try { bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => { versionCode = data.versionName.toString(); console.info(‘versionCode版本号为:.’,versionCode) headerDic.set(‘osVersion’,versionCode); }) } catch (err) { } let versionId = deviceInfo.versionId; headerDic.set(‘osType’,‘01’); headerDic.set(‘appVersion’, versionId); let aaid = ‘’ try { AAID.getAAID((err: BusinessError, data: string) => { if (err) { hilog.error(0x0000, ‘testTag’, ‘Get AAID failed: %{public}d %{public}s’, err.code, err.message); } else { hilog.info(0x0000, ‘testTag’, ‘Get AAID successfully: %{public}s’, data); aaid = data; headerDic.set(‘dvImei’, aaid); } }); } catch (err) { let e: BusinessError = err as BusinessError; hilog.error(0x0000, ‘testTag’, ‘Get AAID catch error: %{public}d %{public}s’, e.code, e.message); } let random18Str : number = Math.ceil((Math.pow(10, 18) - 1) * Math.random()); console.info(‘生成的18位随机数字段.’,random18Str.toString()) headerDic.set(‘nonceStr’,dateStr + random18Str.toString()); headerDic.set(‘txnTm’,dateStr); headerDic.set(‘appChlNo’,‘04’); console.info(‘abc1234,’,headerDic) return headerDic } catch (err) { console.info(‘abc1234’,err) return ‘’ } } 控制台输出 05-27 09:39:23.423 33199-33199 A03D00/JSAPP com.scrcu…rHarmony I abc1234, [object HashMap] 05-27 09:39:23.450 33199-33199 A03D00/JSAPP com.scrcu…rHarmony I Succeeded in jumping to the PatternLockController. 05-27 09:39:23.451 33199-33199 A03D00/JSAPP com.scrcu…rHarmony I versionCode版本号为:. 1.0.0 05-27 09:39:23.457 33199-33199 A00000/testTag com.scrcu…rHarmony I Get AAID successfully: 1459732c-12d9-4199-b7f3-5a1a6463d860更多关于HarmonyOS 鸿蒙Next try catch是异步执行么?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
getBundleInfoForSelf 和 getAAID 都是异步方法,可以使用 await 实现同步,如下:
let versionCode: string = '';
// 更改
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA;
try {
let bundleInfo = await bundleManager.getBundleInfoForSelf(bundleFlags);
versionCode = bundleInfo.versionName.toString();
console.info('versionCode版本号为:. ', versionCode);
headerDic.set('osVersion', versionCode);
} catch (err) {}
let versionId: string = deviceInfo.versionId;
headerDic.set('osType', '01');
headerDic.set('appVersion', versionId);
try {
let aaid: string = await AAID.getAAID();
if (aaid) {
headerDic.set('dvImei', aaid);
}
} catch (err) {
let e: BusinessError = err as BusinessError;
hilog.error(0x0000, 'testTag', 'Get AAID catch error: %{public}d %{public}s', e.code, e.message);
}
更多关于HarmonyOS 鸿蒙Next try catch是异步执行么?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS 鸿蒙中的try-catch
语句块并非用于异步执行。try-catch
是编程中常见的异常处理机制,用于捕获和处理在try
代码块中可能发生的异常。在鸿蒙系统中,这一机制遵循标准的异常处理流程,即在try
块中的代码执行过程中,如果遇到异常,程序会立即跳转到相应的catch
块进行处理,而不会等待任何异步操作。
具体来说,try-catch
的执行是同步的,意味着当异常发生时,程序会立即中断当前try
块中的执行流程,并根据异常类型跳转到匹配的catch
块。如果try
块中没有发生异常,则catch
块将被跳过,程序继续执行try-catch
之后的代码。
异步执行通常涉及不同的线程或任务,这些任务可以在后台独立运行,并在完成时通知主线程。鸿蒙系统提供了多种异步编程机制,如任务调度、异步回调等,但这些机制与try-catch
的异常处理机制是独立的。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html