Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / C#

throw: referencia práctica

throw: referencia práctica: throw lanza una excepción; un throw solo dentro de catch la relanza conservando la pila.

Versión: C# language; verified against C# 14 reference

Sintaxis

throw /* grammar-specific form */

throw lanza una excepción; un throw solo dentro de catch la relanza conservando la pila.

Usa una excepción concreta para una infracción que la función actual no pueda manejar.

throw ex reinicia el punto aparente y las excepciones no deben guiar el flujo normal.

Una expresión throw puede aparecer en ?? o ?:; lanzar null produce NullReferenceException.

El programa completo usa realmente throw; su stdout esperado exacto es boom.

Ejemplos

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

Resultado

boom

El programa completo usa realmente `throw`; su stdout esperado exacto es `boom`.

Fuentes