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




















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var