Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / C#

throw: riferimento pratico

throw: riferimento pratico: throw genera un’eccezione; un throw isolato in catch la rilancia conservando lo stack.

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

Sintassi

throw /* grammar-specific form */

throw genera un’eccezione; un throw isolato in catch la rilancia conservando lo stack.

Usa un’eccezione specifica per una violazione che la funzione corrente non può gestire.

throw ex reimposta il punto apparente e le eccezioni non devono guidare il flusso normale.

Un’espressione throw può comparire in ?? o ?:; lanciare null produce NullReferenceException.

Il programma completo usa realmente throw; lo stdout esatto atteso è boom.

Esempi

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

Risultato

boom

Il programma completo usa realmente `throw`; lo stdout esatto atteso è `boom`.

Fonti