If the network is unavailable, the connection information of the network corresponding to the specified nethandle cannot be parsed when getConnectionPropertiesSync is called, and an error is reported. You can use the try-catch method to obtain the error information. The code snippet is as follows:
import { connection } from '@kit.NetworkKit'
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct GetErrInfo {
getErrInfo() {
try {
let netHandle = connection.getDefaultNetSync();
let connectionproperties = connection.getConnectionPropertiesSync(netHandle);
} catch (err) {
let error: BusinessError = err as BusinessError;
console.log('error: ' + JSON.stringify(error));
}
}
build() {
Row() {
Column() {
Button('Obtain Network Type')
.onClick(() => {
this.getErrInfo();
})
}
.width('100%')
}
.height('100%')
}
}