[dart] try-catch 문

try { // Potentially error-prone code int result = 10 ~/ 0; // This will throw a “IntegerDivisionByZeroException” error print(‘The result is $result’); } catch (e) { // Catch the error and handle it print(‘Error occurred: $e’); } ```

Dart에서 try-catch 문을 사용하여 예외를 처리할 수 있습니다. try 블록 안에 예외가 발생할 수 있는 코드를 작성하고, 발생할 수 있는 예외를 처리할 catch 블록을 작성합니다. 만약 try 블록 안에서 예외가 발생하면, 해당 예외를 catch 블록에서 처리할 수 있습니다.

위의 예시에서는 try 블록 안에서 0으로 나누는 연산을 수행하고 있습니다. 이 코드는 오류를 발생시키며, 이를 catch 블록에서 예외처리하고 메시지를 출력하는 방식으로 처리하고 있습니다.