當前位置: 首頁>>代碼示例>>C#>>正文


C# ArgumentException類代碼示例

本文整理匯總了C#中System.ArgumentException的典型用法代碼示例。如果您正苦於以下問題:C# ArgumentException類的具體用法?C# ArgumentException怎麽用?C# ArgumentException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ArgumentException類屬於System命名空間,在下文中一共展示了ArgumentException類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;

public class Example 
{
    static void Main() 
    {
        // Define some integers for a division operation.
        int[] values = { 10, 7 };
        foreach (var value in values) {
            try {
               Console.WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value));
            }
            catch (ArgumentException e) {
               Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
            }
            Console.WriteLine();
        }
    }

    static int DivideByTwo(int num) 
    {
        // If num is an odd number, throw an ArgumentException.
        if ((num & 1) == 1)
            throw new ArgumentException(String.Format("{0} is not an even number", num), 
                                      "num");

        // num is even, return half of its value.
        return num / 2;
    }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:31,代碼來源:ArgumentException

輸出:

10 divided by 2 is 5

ArgumentException: 7 is not an even number
Parameter name: num

示例2: new ArgumentException()

//引入命名空間
using System;
using System.Collections.Generic;
using System.Text;

public struct Point : IComparable {
    private int x, y;
    public Point(int xPos, int yPos) {
        x = xPos;
        y = yPos;
    }
    public static Point operator +(Point p1, Point p2) { return new Point(p1.x + p2.x, p1.y + p2.y); }
    public static Point operator -(Point p1, Point p2) { return new Point(p1.x - p2.x, p1.y - p2.y); }
    public static bool operator ==(Point p1, Point p2) { return p1.Equals(p2); }
    public static bool operator !=(Point p1, Point p2) { return !p1.Equals(p2); }
    public static bool operator <(Point p1, Point p2) { return (p1.CompareTo(p2) < 0); }
    public static bool operator >(Point p1, Point p2) { return (p1.CompareTo(p2) > 0); }
    public static bool operator <=(Point p1, Point p2) { return (p1.CompareTo(p2) <= 0); }
    public static bool operator >=(Point p1, Point p2) { return (p1.CompareTo(p2) >= 0); }
    public static Point operator ++(Point p1) { return new Point(p1.x + 1, p1.y + 1); }
    public static Point operator --(Point p1) { return new Point(p1.x - 1, p1.y - 1); }
    public override bool Equals(object o) {
        if (o is Point) {
            if (((Point)o).x == this.x &&
                ((Point)o).y == this.y)
                return true;
        }
        return false;
    }

    public override int GetHashCode() { return this.ToString().GetHashCode(); }
    public override string ToString() {
        return string.Format("[{0}, {1}]", this.x, this.y);
    }
    public int CompareTo(object obj) {
        if (obj is Point) {
            Point p = (Point)obj;
            if (this.x > p.x && this.y > p.y)
                return 1;
            if (this.x < p.x && this.y < p.y)
                return -1;
            else
                return 0;
        } else
            throw new ArgumentException();
    }
    public static Point Add(Point p1, Point p2) { return p1 + p2; }
    public static Point Subtract(Point p1, Point p2) { return p1 - p2; }
}
class Program {
    static void Main(string[] args) {
        Point ptOne = new Point(100, 100);
        Point ptTwo = new Point(40, 40);
        Console.WriteLine("ptOne = {0}", ptOne);
        Console.WriteLine("ptTwo = {0}", ptTwo);
        Console.WriteLine("ptOne + ptTwo: {0} ", ptOne + ptTwo);
        Console.WriteLine("Point.Add(ptOne, ptTwo): {0} ", Point.Add(ptOne, ptTwo));

        Console.WriteLine("ptOne - ptTwo: {0} ", ptOne - ptTwo);
        Console.WriteLine("Point.Subtract(ptOne, ptTwo): {0} ", Point.Subtract(ptOne, ptTwo));

        Point ptThree = new Point(90, 5);
        Console.WriteLine("ptThree = {0}", ptThree);
        Console.WriteLine("ptThree += ptTwo: {0}", ptThree += ptTwo);

        Point ptFour = new Point(0, 500);
        Console.WriteLine("ptFour = {0}", ptFour);
        Console.WriteLine("ptFour -= ptThree: {0}", ptFour -= ptThree);

        Point ptFive = new Point(10, 10);
        Console.WriteLine("ptFive = {0}", ptFive);
        Console.WriteLine("++ptFive = {0}", ++ptFive);
        Console.WriteLine("--ptFive = {0}", --ptFive);

        Console.WriteLine("ptOne == ptTwo : {0}", ptOne == ptTwo);
        Console.WriteLine("ptOne != ptTwo : {0}", ptOne != ptTwo);
        Console.WriteLine("ptOne < ptTwo : {0}", ptOne < ptTwo);
        Console.WriteLine("ptOne > ptTwo : {0}", ptOne > ptTwo);
    }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:80,代碼來源:ArgumentException


注:本文中的System.ArgumentException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。