try catch finally and arrays in csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace try_catch_array
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] sayi1=new int[5];
int[] sayi2=new int[5];
int i = 0,j=0;
private void btnEkleSayi1_Click(object sender, EventArgs e)
{
string sayi = ".sayi";
sayi1[i] =int.Parse(txtSayi1.Text);
txtList.Text = txtList.Text + (i+1).ToString()+sayi +"\t"+ sayi1[i].ToString()+ Environment.NewLine;
txtSayi1.Clear();
txtSayi1.Focus();
i++;
}
private void btnEkleSayi2_Click(object sender, EventArgs e)
{
string bol = ".bolen";
sayi2[j] = int.Parse(txtSayi2.Text);
txtList.Text = txtList.Text + (j + 1).ToString()+bol + "\t" + sayi2[j].ToString() + Environment.NewLine;
txtSayi2.Clear();
txtSayi2.Focus();
j++;
}
private void btnStart_Click(object sender, EventArgs e)
{
try // try to do it // gerçekleþtirmeyi dene
{
string result = "result ";
for (i = 0; i < sayi1.Length; i++)
{
txtList.Text = txtList.Text + result + "\t" + (sayi1[i] / sayi2[i]).ToString() + Environment.NewLine;
}
}
catch(Exception ex) //catch the error // hata yakala
{
MessageBox.Show(ex.Message);
}
finally // after error message, do that too // hatadan sonra bunu da yap!
{
MessageBox.Show("gives error because we defined array[5] but we entered less then five");
}
}
}
}