Parsing : Exceptions

CSharp · Console

/Program.cs

using System;

public class Program
{
	public static void Main()
	{
		string val = "786";
		Console.WriteLine(Int32.Parse(val)); // Return 786 
		
		
		// Ex 1 : Remove 3 lines below to execute Ex 2
		
		
		//FormatException -> 'Input string was not in a correct format.'
		string val1 = "786.78";
		Console.WriteLine(Int32.Parse(val1));
		
		
		// Ex 2 : Remove 3 lines below to execute Ex 3
		
		 				 
		//ArgumentNullException -> 'Value cannot be null.'
		string val2 = null;
		Console.WriteLine(Int32.Parse(val2));

		
		
		// Ex 3
		
		
		//OverflowException -> 'Value was either too large or too small for an Int32.’.
		string val3 = "786786786786";
		Console.WriteLine(Int32.Parse(val3));



	}
}
;