[ExceptionHandling] Øvelse i exception handling: Byte array
This commit is contained in:
parent
f1bf7cb53b
commit
26fe008193
@ -62,6 +62,14 @@
|
||||
this.btnBmi = new System.Windows.Forms.Button();
|
||||
this.lblBmi = new System.Windows.Forms.Label();
|
||||
this.txtBmi = new System.Windows.Forms.TextBox();
|
||||
this.lblExceptionOppgaveOne = new System.Windows.Forms.Label();
|
||||
this.btnExceptionToArray = new System.Windows.Forms.Button();
|
||||
this.lblExceptionIndexNum = new System.Windows.Forms.Label();
|
||||
this.txtExceptionIndexNum = new System.Windows.Forms.TextBox();
|
||||
this.txtExceptionValueOne = new System.Windows.Forms.TextBox();
|
||||
this.lblExceptionValueOne = new System.Windows.Forms.Label();
|
||||
this.lblExceptionArrayValues = new System.Windows.Forms.Label();
|
||||
this.txtExceptionArrayValues = new System.Windows.Forms.TextBox();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// txtExVat
|
||||
@ -350,11 +358,92 @@
|
||||
this.txtBmi.Size = new System.Drawing.Size(100, 20);
|
||||
this.txtBmi.TabIndex = 32;
|
||||
//
|
||||
// lblExceptionOppgaveOne
|
||||
//
|
||||
this.lblExceptionOppgaveOne.AutoSize = true;
|
||||
this.lblExceptionOppgaveOne.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.lblExceptionOppgaveOne.Location = new System.Drawing.Point(13, 315);
|
||||
this.lblExceptionOppgaveOne.Name = "lblExceptionOppgaveOne";
|
||||
this.lblExceptionOppgaveOne.Size = new System.Drawing.Size(129, 13);
|
||||
this.lblExceptionOppgaveOne.TabIndex = 33;
|
||||
this.lblExceptionOppgaveOne.Text = "Exception Oppgave 1";
|
||||
//
|
||||
// btnExceptionToArray
|
||||
//
|
||||
this.btnExceptionToArray.Location = new System.Drawing.Point(16, 341);
|
||||
this.btnExceptionToArray.Name = "btnExceptionToArray";
|
||||
this.btnExceptionToArray.Size = new System.Drawing.Size(75, 23);
|
||||
this.btnExceptionToArray.TabIndex = 34;
|
||||
this.btnExceptionToArray.Text = "Til array";
|
||||
this.btnExceptionToArray.UseVisualStyleBackColor = true;
|
||||
this.btnExceptionToArray.Click += new System.EventHandler(this.btnExceptionToArray_Click);
|
||||
//
|
||||
// lblExceptionIndexNum
|
||||
//
|
||||
this.lblExceptionIndexNum.AutoSize = true;
|
||||
this.lblExceptionIndexNum.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.lblExceptionIndexNum.ForeColor = System.Drawing.Color.Blue;
|
||||
this.lblExceptionIndexNum.Location = new System.Drawing.Point(107, 346);
|
||||
this.lblExceptionIndexNum.Name = "lblExceptionIndexNum";
|
||||
this.lblExceptionIndexNum.Size = new System.Drawing.Size(92, 13);
|
||||
this.lblExceptionIndexNum.TabIndex = 35;
|
||||
this.lblExceptionIndexNum.Text = "Indeksnummer:";
|
||||
//
|
||||
// txtExceptionIndexNum
|
||||
//
|
||||
this.txtExceptionIndexNum.Location = new System.Drawing.Point(205, 343);
|
||||
this.txtExceptionIndexNum.Name = "txtExceptionIndexNum";
|
||||
this.txtExceptionIndexNum.Size = new System.Drawing.Size(100, 20);
|
||||
this.txtExceptionIndexNum.TabIndex = 36;
|
||||
//
|
||||
// txtExceptionValueOne
|
||||
//
|
||||
this.txtExceptionValueOne.Location = new System.Drawing.Point(522, 343);
|
||||
this.txtExceptionValueOne.Name = "txtExceptionValueOne";
|
||||
this.txtExceptionValueOne.Size = new System.Drawing.Size(100, 20);
|
||||
this.txtExceptionValueOne.TabIndex = 38;
|
||||
//
|
||||
// lblExceptionValueOne
|
||||
//
|
||||
this.lblExceptionValueOne.AutoSize = true;
|
||||
this.lblExceptionValueOne.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.lblExceptionValueOne.ForeColor = System.Drawing.Color.Blue;
|
||||
this.lblExceptionValueOne.Location = new System.Drawing.Point(318, 346);
|
||||
this.lblExceptionValueOne.Name = "lblExceptionValueOne";
|
||||
this.lblExceptionValueOne.Size = new System.Drawing.Size(198, 13);
|
||||
this.lblExceptionValueOne.TabIndex = 37;
|
||||
this.lblExceptionValueOne.Text = "Oppgi tall som skal legges i array:";
|
||||
//
|
||||
// lblExceptionArrayValues
|
||||
//
|
||||
this.lblExceptionArrayValues.AutoSize = true;
|
||||
this.lblExceptionArrayValues.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.lblExceptionArrayValues.Location = new System.Drawing.Point(13, 384);
|
||||
this.lblExceptionArrayValues.Name = "lblExceptionArrayValues";
|
||||
this.lblExceptionArrayValues.Size = new System.Drawing.Size(85, 13);
|
||||
this.lblExceptionArrayValues.TabIndex = 39;
|
||||
this.lblExceptionArrayValues.Text = "Array-innhold:";
|
||||
//
|
||||
// txtExceptionArrayValues
|
||||
//
|
||||
this.txtExceptionArrayValues.Location = new System.Drawing.Point(105, 384);
|
||||
this.txtExceptionArrayValues.Name = "txtExceptionArrayValues";
|
||||
this.txtExceptionArrayValues.Size = new System.Drawing.Size(517, 20);
|
||||
this.txtExceptionArrayValues.TabIndex = 40;
|
||||
//
|
||||
// OvingsOppgave
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||
this.Controls.Add(this.txtExceptionArrayValues);
|
||||
this.Controls.Add(this.lblExceptionArrayValues);
|
||||
this.Controls.Add(this.txtExceptionValueOne);
|
||||
this.Controls.Add(this.lblExceptionValueOne);
|
||||
this.Controls.Add(this.txtExceptionIndexNum);
|
||||
this.Controls.Add(this.lblExceptionIndexNum);
|
||||
this.Controls.Add(this.btnExceptionToArray);
|
||||
this.Controls.Add(this.lblExceptionOppgaveOne);
|
||||
this.Controls.Add(this.txtBmi);
|
||||
this.Controls.Add(this.lblBmi);
|
||||
this.Controls.Add(this.btnBmi);
|
||||
@ -434,6 +523,14 @@
|
||||
private System.Windows.Forms.Button btnBmi;
|
||||
private System.Windows.Forms.Label lblBmi;
|
||||
private System.Windows.Forms.TextBox txtBmi;
|
||||
private System.Windows.Forms.Label lblExceptionOppgaveOne;
|
||||
private System.Windows.Forms.Button btnExceptionToArray;
|
||||
private System.Windows.Forms.Label lblExceptionIndexNum;
|
||||
private System.Windows.Forms.TextBox txtExceptionIndexNum;
|
||||
private System.Windows.Forms.TextBox txtExceptionValueOne;
|
||||
private System.Windows.Forms.Label lblExceptionValueOne;
|
||||
private System.Windows.Forms.Label lblExceptionArrayValues;
|
||||
private System.Windows.Forms.TextBox txtExceptionArrayValues;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,11 +22,16 @@ namespace OvingsOppgaveTre
|
||||
double exVat;
|
||||
string exVatText = txtExVat.Text;
|
||||
|
||||
if (double.TryParse(exVatText, out exVat))
|
||||
try
|
||||
{
|
||||
exVat = Convert.ToDouble(exVatText);
|
||||
double incVat = exVat * 1.25;
|
||||
txtIncVat.Text = incVat.ToString();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private void txtIncVat_TextChanged(object sender, EventArgs e)
|
||||
@ -93,5 +98,39 @@ namespace OvingsOppgaveTre
|
||||
txtBmi.Text = bmi.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private void btnExceptionToArray_Click(object sender, EventArgs e)
|
||||
{
|
||||
byte[] byteArray = new byte[6] {0, 0, 0, 0, 0, 0};
|
||||
var indexText = txtExceptionIndexNum.Text;
|
||||
var arrayValueText = txtExceptionValueOne.Text;
|
||||
|
||||
try
|
||||
{
|
||||
var indexNum = Convert.ToInt16(indexText);
|
||||
var arrayValue = Convert.ToByte(arrayValueText);
|
||||
|
||||
byteArray[indexNum] = arrayValue;
|
||||
|
||||
txtExceptionArrayValues.Text = string.Join("\t", byteArray);
|
||||
}
|
||||
catch (IndexOutOfRangeException ex)
|
||||
{
|
||||
MessageBox.Show($"Ugyldig inndeks spesifisert i array: {indexText}\n{ex.GetType()}");
|
||||
}
|
||||
catch (OverflowException ex)
|
||||
{
|
||||
MessageBox.Show(
|
||||
$"Tallet du spesifiserte er for høyt for typen `byte`: {arrayValueText}\n{ex.GetType()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
MessageBox.Show("Test utført.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user