You are on page 1of 4

MUHAMMAD HARIS BIN HAIRUDDEEN D032110464 S1G1

WEEK 5 LAB ASSESMENT


QUESTION 1 (Steps 4, 5, 6 and 7)

step 5
MUHAMMAD HARIS BIN HAIRUDDEEN D032110464 S1G1

step 6

Step 7
MUHAMMAD HARIS BIN HAIRUDDEEN D032110464 S1G1

Question 2
catch (FormatException excep)
{
MessageBox.Show("Invalid input.integers only.");
}
catch (DivideByZeroException excep)
{
MessageBox.Show("Division by zero is not allowed.");
}
catch (Exception error)
{
MessageBox.Show("An error occurred: ");
}

Question 3
a) The second catch will never be read.

b) Because the first catch is first one to encounter an exception and also it not a general
exception and second exception will not be executed as because the first one is
there. Moreover, exceptions must be in order.

c) The DivideByZero exception must be the first exception to accept then the other one
as below:
catch (DivideByZeroException excep)
{
MessageBox.Show(excep.Message);
}
catch (Exception excep)
{
MessageBox.Show(excep.Message);
}

Question 4
a)
try
{
MUHAMMAD HARIS BIN HAIRUDDEEN D032110464 S1G1

if (comboBox1.SelectedIndex >= 0)
{
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
}
else
{
MessageBox.Show("No more items to delete");
}
}
catch (ArgumentOutOfRangeException)
{
MessageBox.Show("No more items to delete");

b)

try
{
if (comboBox1.SelectedIndex >= 0)
{
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
if (comboBox1.Items.Count == 0)
{
deleteButton.Enabled = false;
comboBox1.Enabled = false;
}
}
else
{
MessageBox.Show("No more items to delete");
}
}
catch (ArgumentOutOfRangeException)
{
MessageBox.Show("No more items to delete");
deleteButton.Enabled = false;
comboBox1.Enabled = false;
}

You might also like