CKTIS / C#
error handling: referência prática
error handling: referência prática: Os erros são representados por exceções específicas ou resultados de domínio conforme a previsibilidade.
Versão: C# language and .NET 10 reference runtime
Sintaxe
// errorsOs erros são representados por exceções específicas ou resultados de domínio conforme a previsibilidade.
Captura num limite onde possas recuperar, adicionar contexto ou traduzir o erro.
Um catch vazio esconde falhas; não uses exceções para fluxo de controlo normal.
Finally deve libertar recursos também durante cancelamento e execução parcial.
O programa completo usa realmente error handling; o stdout exato esperado é invalid number.
Exemplos
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
try { _ = int.Parse("x", CultureInfo.InvariantCulture); } catch (FormatException) { Console.WriteLine("invalid number"); }
Resultado
invalid number
O programa completo usa realmente `error handling`; o stdout exato esperado é `invalid number`.