• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

HarmonyOS Next Error Handling: try-catch and Result Type

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
In HarmonyOS Next development, efficient error handling mechanism is the key to ensuring program stability and reliability.Cangjie Language provides try-catch statements and Result types to handle possible errors during program run. Below I will combine actual development experience to analyze these two error handling methods in depth.

1. Anomaly classification system

(I) Tested exception vs. Untested exception design philosophy


In Cangjie language, exceptions are divided into checked exceptions and unchecked exceptions.The detected exception requires the developer to explicitly handle it in the code, and the compiler will force the check; the non-checked exception usually indicates a logical error in the program and does not require explicit processing, but may cause the program to crash.

The detected exception is suitable for those error scenarios that are foreseeable and need to be processed, such as the file does not exist when the file is read, the network is not available when the network is requested, etc.By forcibly handling the detected exception, we can ensure that the program can maintain a certain stability when facing these errors.Take file reading as an example:


try {
let file = File("nonexistent.txt")
let content = file.readText()
println(content)
} catch (e: FileNotFoundException) {
println("The file does not exist: \(e.message)")
} catch (e: IOException) {
println("Error reading file: \(e.message)")
}

In this code, FileNotFoundException and IOException are detected exceptions. The compiler will check whether these exceptions have been processed to avoid abnormal termination of the program due to unhandled exceptions.

Unchecked exceptions are used to indicate logical errors within the program, such as null pointer references, array out of bounds, etc.These exceptions usually mean that there are vulnerabilities in the program and developers need to fix the code logic rather than simply perform exception handling.For example:


let numbers = [1, 2, 3]
let outOfBoundsIndex = 5
let value = numbers[outOfBoundsIndex] // IndexOutOfBoundsException will be thrown here, which is an unchecked exception

This design philosophy allows developers to distinguish errors of different natures, process them in a targeted manner, and improve the robustness of their code.

2. Error recovery strategy

(I) Implementation of file reading retry mechanism


In actual development, file reading operations may fail for various reasons, such as temporary inaccessibility of files, disk I/O errors, etc.In order to improve the fault tolerance of the program, a retry mechanism for file reading can be implemented.Use try-catch to combine loop structure, as shown below:


func readFileWithRetry(filePath: String, maxRetries: Int = 3) -> String? {
var retryCount = 0
while (retryCount < maxRetries) {
try {
let file = File(filePath)
return file.readText()
} catch (e: FileNotFoundException) {
println("The file does not exist: \(e.message)")
break
} catch (e: IOException) {
println("Error reading file: \(e.message), try again...")
retryCount++
}
}
return null
}

In the above code, the readFileWithRetry function will try to read the file. If you encounter IOException, you will try again, and then try maxRetries at most.If it is FileNotFoundException, then try again directly.This retry mechanism can improve the success rate of file reading operations to a certain extent and enhance the stability of the program.

3. Performance impact assessment

(I) Stack expansion overhead test for exception capture


Exception capture will bring certain performance overhead, which is mainly reflected in the stack unwinding process.When an exception is thrown, the system needs to trace back along the call stack and release resources on the stack, which will consume a certain amount of time and resources.To evaluate this overhead, simple performance testing can be performed:


import std.time.*

func performTask() {
try {
// Simulate an operation that may throw an exception
let result = divide(10, 0)
} catch (e: ArithmeticException) {
//Catch exception
}
}

func divide(a: Int, b: Int) -> Int {
if (b == 0) {
throw ArithmeticException("The divisor cannot be zero")
}
return a / b
}

let startTime = getCurrentTime()
for (i in 0..100000) {
performTask()
}
let endTime = getCurrentTime()
let elapsedTime = endTime - startTime
println("Time to perform 100,000 exception captures: \(elapsedTime) ms")

Through the above test code, it can be found that the overhead of exception capture is related to factors such as the depth of the call stack, the number of resources on the stack, etc.In actual development, frequent use of exception capture in performance-critical code paths should be avoided, and try to use it when the probability of exception occurrence is low to reduce the impact on program performance.

Mastering how to use try-catch and Result types, as well as understanding the performance impact of exception handling, helps developers write more robust and efficient HarmonyOS Next applications.In actual projects, appropriate error handling strategies should be selected according to specific scenarios to balance the stability and performance of the program.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу