עבור לתוכן

Exception ב-C#

Featured Replies

פורסם

בקוד הבא אני עושה בפקודה הראשונה ב-MAIN שגיאה שנתפסת ב-Catch, השאלה שלי היא איך אני יכול שהשגיאה תיתפס אבל שאר הפקודות אחריה יתבצעו.

בעיקרון מה שאני עושה זה נותן לאובייקט של Time פרמטר לא נכון של דק' (מעל 59)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Time
{
public class IncorrectTimeParameters : ApplicationException
{
public IncorrectTimeParameters(string message)
: base(message)
{
}
}


public class Time : IComparable, ICloneable
{
protected int HH;
protected int MM;
protected int SS;


public Time(int h, int m, int s)
{
if (h <= 0 || h > 24)
{
throw new IncorrectTimeParameters("Incorrect Hours Value: " + h);
}


if (m < 0 || m > 59)
{
throw new IncorrectTimeParameters("Incorrect Minutes Value: " + m);
}


if (s >= 86400)
{
s -= 86400;
}


this.HH = h;
this.MM = m;
this.SS = s;
}


public Time(int seconds)
{
if (seconds < 0)
{
throw new IncorrectTimeParameters("Incorrect Minutes Value: " + seconds);
}


else if (seconds >= 86400)
{
seconds %= 86400;
}


this.HH = seconds / 3600;
seconds %= 3600;
this.MM = seconds / 60;
seconds %= 60;
this.SS = seconds;
}


public Time(Time other)
{
if (HH <= 0 || HH > 24)
{
throw new IncorrectTimeParameters("Incorrect Hours Value: " + HH);
}


if (MM < 0 || MM > 59)
{
throw new IncorrectTimeParameters("Incorrect Minutes Value: " + MM);
}


if (SS >= 86400)
{
SS -= 86400;
}


this.HH = other.HH;
this.MM = other.MM;
this.SS = other.SS;
}


public override string ToString()
{
if (this.MM < 10 && this.SS < 10)
{
return "The Time Is: " + this.HH + ":" + "0" + this.MM + ":" + "0" + this.SS;
}
else if (this.SS < 10)
{
return "The Time Is: " + this.HH + ":" + this.MM + ":" + "0" + this.SS;
}
else if (this.MM < 10)
{
return "The Time Is: " + this.HH + ":" + "0" + this.MM + ":" + this.SS;
}
else
{
return "The Time Is: " + this.HH + ":" + this.MM + ":" + this.SS;
}
}


public int ToSeconds()
{
return (this.HH * 3600) + (this.MM * 60) + this.SS;
}


public void FromSeconds(int seconds)
{
if (seconds < 0)
{
throw new IncorrectTimeParameters("Incorrect Seconds Value: " + seconds);
}


else if (seconds >= 86400)
{
seconds -= 86400;
}


this.HH = seconds / 3600;
seconds %= 3600;
this.MM = seconds / 60;
seconds %= 60;
this.SS = seconds;
}


public void Add(Time t2)
{
int PlusTime = this.ToSeconds() + t2.ToSeconds();
this.FromSeconds(PlusTime);
}


public object Clone()
{
return new Time(this.HH, this.MM, this.SS);
}


public int CompareTo(object obj)
{
Time a = (Time)obj;


return this.ToSeconds().CompareTo(a.ToSeconds());
}
}


public class Time12 : Time
{
public Time12(int h, int m, int s)
: base(h, m, s)
{
}


public override string ToString()
{
string Time = "";


if (this.ToSeconds() <= 43200)
{
Time = "AM";
return "The Time Is: " + base.ToString() + " " + Time;
}


else
{
Time = "PM";
return "The Time Is: " + base.ToString() + " " + Time;
}
}


public bool IsAM()
{
return this.ToSeconds() < 46799;
}
}


class Program
{
static void Main(string[] args)
{
try
{
Time t1 = new Time(12, 96, 29);
Console.WriteLine(t1.ToString());


t1.FromSeconds(9000);
Console.WriteLine("The Time Is: " + t1);


int t1Sec = t1.ToSeconds();
Console.WriteLine("The Time In Seconds Is: " + t1Sec);


Time t2 = new Time(23, 10, 12);
Time t3 = new Time(3, 35, 00);


t2.Add(t3);
Console.WriteLine(t2.ToString());


Time12 t4 = new Time12(12, 0, 1);
Console.WriteLine(t4.ToString());
}


catch (IncorrectTimeParameters error)
{
Console.WriteLine(error.Message);
}


Console.ReadLine();
}
}
}

פורסם

איך אתה מצפה שהקוד ימשיך לרוץ (לוגית)? אם החריגה נזרקה באמצע הבנאי של t1, אז האובייקט לא נבנה, והוא לא קיים יותר.

פורסם

לאחר ה- Catch ניתן לכתוב בלוק של Finally שיתבצע תמיד גם אם יש שגיאה.

פורסם

נכון, אבל פותח הת'רד רוצה שהקוד שאחרי הבנאי הבעייתי ימשיך לרוץ. הבעיה היא שהקוד הזה תלוי בתוצאות של הבנאי הזה.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים