Parse string to byte and catch the OverflowException and FormatException in CSharp
Description
The following code shows how to parse string to byte and catch the OverflowException and FormatException.
Example
using System;/*from w ww . ja va2 s.com*/
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);
}
}
}