Try, catch va finally ga doir masalalar yechish. Istisnolarni yuzaga keltirish


-misol. Quyidagi ifodaning qiymatini hisoblovchi dastur tuzing


Download 29.09 Kb.
bet2/2
Sana21.09.2023
Hajmi29.09 Kb.
#1683744
1   2
Bog'liq
31-mavzu. try, catch va finally ga doir masalalar yechish. Istisnolarni yuzaga keltirish

1-misol. Quyidagi ifodaning qiymatini hisoblovchi dastur tuzing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dotnetuz


{
class Program
{
static void Main(string[] args)
{
double s, a, x;
try
{
Console.Write("a=");
a = Double.Parse(Console.ReadLine()); Console.Write("x=");
x = Double.Parse(Console.ReadLine());

s = (a * a + Math.Exp(2 * a - 6)) / (x * (a + Math.Pow(2, x)));


Console.WriteLine("s=" + s);
}
catch (SystemException ex)
{
Console.WriteLine("Ifodaning qiymatini hisoblashda" + ex.Message + " xatolik yuz berdi");
}
finally
{
Console.WriteLine("Ifodaning qiymatini hisoblash tugadi");
}
Console.ReadLine();
}
}
}
2-Misol. sonlar va son berilgan bo’lsa, yig’indini hisoblovchi dastur tuzing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dotnetuz


{
class Program
{
static void Main(string[] args)
{
double[] a = new double[10];
double x, s = 0;
Console.Write("x=");
try
{
x = Double.Parse(Console.ReadLine());
for (int i = 0; i < 10; i++)
{
a[i] = Double.Parse(Console.ReadLine());
if (x + i != 0) { s += a[i] / (x + i); } else throw new DivideByZeroException();
}
Console.WriteLine("s=" + s);
}
catch (DivideByZeroException)
{
Console.WriteLine("Ifodani hisoblashda 0 ga bo'lish uchradi");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Massivning indeksi chegaradan tashqariga chiqdi");
}
catch (SystemException ex)
{
Console.WriteLine("Xatolik:" + ex.Message +" yuz berdi");
}
finally
{
Console.WriteLine("Dastur tugadi");
}
Console.ReadLine();

}
}


}
Download 29.09 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling