FormatException in C#


FomatException is thrown when the format of an argument is invalid.

Let us see an example.

When we set a value other than int to int.Parse() method, then FormatException is thrown as shown below −

Example

 Live Demo

using System;
class Demo {
   static void Main() {
      string str = "3.5";
      int res = int.Parse(str);
   }
}

The following error is thrown when the above program is compiled since we have passed a value other than integer.

Output

Unhandled Exception:
System.FormatException: Input string was not in a correct format.

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements