Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

error handling: practical reference

error handling: practical reference: Errors are represented by specific exceptions or domain results according to predictability.

Version: C# language and .NET 10 reference runtime

Syntax

// errors

Errors are represented by specific exceptions or domain results according to predictability.

Catch at a boundary where you can recover, add context, or translate the error.

An empty catch hides failures; do not use exceptions for ordinary control flow.

Finally must release resources during cancellation and partial execution as well.

The complete program actually uses error handling; its exact expected stdout is invalid number.

Examples

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"); }

Result

invalid number

The complete program actually uses `error handling`; its exact expected stdout is `invalid number`.

Sources