Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / C#

throw: référence pratique

throw: référence pratique: throw lève une exception ; un throw seul dans catch la relance en conservant sa pile.

Version : C# language; verified against C# 14 reference

Syntaxe

throw /* grammar-specific form */

throw lève une exception ; un throw seul dans catch la relance en conservant sa pile.

Utilisez une exception précise pour une violation que la fonction courante ne peut pas traiter.

throw ex réinitialise le site apparent et les exceptions ne doivent pas piloter le flux normal.

Une expression throw peut figurer dans ?? ou ?: ; lancer null produit NullReferenceException.

Le programme complet utilise réellement throw ; sa sortie stdout exacte attendue est boom.

Exemples

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 { throw new Exception("boom"); } catch (Exception error) { Console.WriteLine(error.Message); }

Résultat

boom

Le programme complet utilise réellement `throw` ; sa sortie stdout exacte attendue est `boom`.

Sources