Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C#

System.Exception: referência prática

System.Exception: referência prática: Exception é a base das falhas geridas e contém Message, StackTrace e uma InnerException opcional.

Versão: .NET API; verified on .NET 10.0.14

Sintaxe

System.Exception /* select a documented member or constructor */

Exception é a base das falhas geridas e contém Message, StackTrace e uma InnerException opcional.

Lança um tipo mais específico e adiciona contexto sem perder a exceção interna original.

Não controles a lógica através de Message e usa throw em vez de throw error para preservar a stack.

StackTrace pode ser null antes do lançamento e serializar exceções não é um protocolo estável entre sistemas.

O programa completo usa realmente System.Exception; o stdout exato esperado é boom.

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;

var error = new Exception("boom");
Console.WriteLine(error.Message);

Resultado

boom

O programa completo usa realmente `System.Exception`; o stdout exato esperado é `boom`.

Fontes