Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.FormatException: practical reference

System.FormatException: practical reference: FormatException reports text that does not conform to the syntax expected by a parser or formatter.

Version: .NET API; verified on .NET 10.0.14

Syntax

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

FormatException reports text that does not conform to the syntax expected by a parser or formatter.

Use TryParse for predictable user input and reserve the exception for a violated programming contract.

Culture and NumberStyles are part of the format, so the same text can have different meanings.

Empty or whitespace text usually fails parsing, while an out-of-range number raises OverflowException instead.

The complete program actually uses System.FormatException; its exact expected stdout is FormatException.

Examples

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

Result

FormatException

The complete program actually uses `System.FormatException`; its exact expected stdout is `FormatException`.

Sources