Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / C#

System.FormatException: riferimento pratico

System.FormatException: riferimento pratico: FormatException segnala testo non conforme alla sintassi attesa da parser o formattatore.

Versione: .NET API; verified on .NET 10.0.14

Sintassi

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

FormatException segnala testo non conforme alla sintassi attesa da parser o formattatore.

Usa TryParse per input utente prevedibile e riserva l’eccezione a una violazione del contratto di programmazione.

Cultura e NumberStyles fanno parte del formato, quindi lo stesso testo può avere significati diversi.

Testo vuoto o composto da spazi in genere fallisce; un numero fuori intervallo genera invece OverflowException.

Il programma completo usa realmente System.FormatException; lo stdout esatto atteso è FormatException.

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 { int.Parse("x", CultureInfo.InvariantCulture); } catch (FormatException error) { Console.WriteLine(error.GetType().Name); }

Risultato

FormatException

Il programma completo usa realmente `System.FormatException`; lo stdout esatto atteso è `FormatException`.

Fonti