Mega Code Archive

 
Categories / C# / Data Types
 

Parse string to byte and catch the OverflowException and FormatException

using System; public class Example {    public static void Main()    {                  string string1 = "244";         try {            byte byte1 = Byte.Parse(string1);            Console.WriteLine(byte1);         }         catch (OverflowException) {            Console.WriteLine("'{0}' is out of range of a byte.", string1);         }         catch (FormatException) {            Console.WriteLine("'{0}' is out of range of a byte.", string1);         }     } }