Обработка исключений

This commit is contained in:
Павел Ладягин 2024-04-20 10:04:26 +04:00
parent cdaae7bc7d
commit 923aafca36
4 changed files with 126 additions and 75 deletions

View File

@ -1,4 +1,6 @@

using ProjectAirplaneWithRadar.Exceptions;
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
{
/// <summary>
@ -48,7 +50,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
public T? Get(int position)
{
if (position >= Count || position < 0)
return null;
throw new PositionOutOfCollectionException(position);
return _collection[position];
}
@ -56,7 +58,8 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
public int Insert(T obj)
{
if (Count + 1 > _maxCount)
return -1;
throw new CollectionOverflowException(Count);
_collection.Add(obj);
return Count;
}
@ -64,9 +67,11 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
public int Insert(T obj, int position)
{
if (Count + 1 > _maxCount)
return -1;
throw new CollectionOverflowException(Count);
if (position < 0 || position > Count)
return -1;
throw new PositionOutOfCollectionException(position);
_collection.Insert(position, obj);
return 1;
}
@ -74,7 +79,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
public T? Remove(int position)
{
if (position < 0 || position > Count)
return null;
throw new PositionOutOfCollectionException(position);
T? temp = _collection[position];
_collection.RemoveAt(position);

View File

@ -1,4 +1,6 @@

using ProjectAirplaneWithRadar.Exceptions;
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
{
/// <summary>
@ -51,7 +53,11 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
public T? Get(int position)
{
if (position < 0 || position >= Count)
return null;
throw new PositionOutOfCollectionException(position);
if (position >= _collection.Length && _collection[position] == null)
throw new ObjectNotFoundException(position);
return _collection[position];
}
@ -65,13 +71,13 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
return i;
}
}
return -1;
throw new CollectionOverflowException(Count);
}
public int Insert(T obj, int position)
{
if (position < 0 || position >= Count)
return -1;
throw new PositionOutOfCollectionException(position);
if (_collection[position] == null)
{
@ -100,19 +106,16 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects
}
temp--;
}
return -1;
throw new CollectionOverflowException(Count);
}
public T? Remove(int position)
{
if (position < 0 || position >= Count)
return null;
throw new PositionOutOfCollectionException(position);
if (_collection[position] == null)
{
return null;
}
throw new ObjectNotFoundException(position);
T? temp = _collection[position];
_collection[position] = null;

View File

@ -66,9 +66,11 @@
groupBoxTools.Controls.Add(panelStorage);
groupBoxTools.Controls.Add(comboBoxSelectorCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(883, 24);
groupBoxTools.Location = new Point(855, 30);
groupBoxTools.Margin = new Padding(3, 4, 3, 4);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(206, 583);
groupBoxTools.Padding = new Padding(3, 4, 3, 4);
groupBoxTools.Size = new Size(235, 779);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
@ -82,17 +84,19 @@
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 393);
panelCompanyTools.Location = new Point(3, 526);
panelCompanyTools.Margin = new Padding(3, 4, 3, 4);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(200, 187);
panelCompanyTools.Size = new Size(229, 249);
panelCompanyTools.TabIndex = 8;
//
// buttonAddAirplane
//
buttonAddAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddAirplane.Location = new Point(0, 3);
buttonAddAirplane.Location = new Point(0, 4);
buttonAddAirplane.Margin = new Padding(3, 4, 3, 4);
buttonAddAirplane.Name = "buttonAddAirplane";
buttonAddAirplane.Size = new Size(97, 56);
buttonAddAirplane.Size = new Size(111, 75);
buttonAddAirplane.TabIndex = 1;
buttonAddAirplane.Text = "Добавить самолет";
buttonAddAirplane.UseVisualStyleBackColor = true;
@ -100,19 +104,21 @@
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(0, 81);
maskedTextBoxPosition.Location = new Point(0, 108);
maskedTextBoxPosition.Margin = new Padding(3, 4, 3, 4);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(78, 23);
maskedTextBoxPosition.Size = new Size(89, 27);
maskedTextBoxPosition.TabIndex = 3;
maskedTextBoxPosition.ValidatingType = typeof(int);
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(103, 123);
buttonRefresh.Location = new Point(118, 164);
buttonRefresh.Margin = new Padding(3, 4, 3, 4);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(97, 56);
buttonRefresh.Size = new Size(111, 75);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
@ -121,9 +127,10 @@
// buttonRemoveAirplane
//
buttonRemoveAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRemoveAirplane.Location = new Point(103, 70);
buttonRemoveAirplane.Location = new Point(118, 93);
buttonRemoveAirplane.Margin = new Padding(3, 4, 3, 4);
buttonRemoveAirplane.Name = "buttonRemoveAirplane";
buttonRemoveAirplane.Size = new Size(97, 43);
buttonRemoveAirplane.Size = new Size(111, 57);
buttonRemoveAirplane.TabIndex = 4;
buttonRemoveAirplane.Text = "Удалить самолет";
buttonRemoveAirplane.UseVisualStyleBackColor = true;
@ -132,9 +139,10 @@
// buttonGoToCheck
//
buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToCheck.Location = new Point(0, 123);
buttonGoToCheck.Location = new Point(0, 164);
buttonGoToCheck.Margin = new Padding(3, 4, 3, 4);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(97, 56);
buttonGoToCheck.Size = new Size(111, 75);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
@ -142,9 +150,10 @@
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(6, 305);
buttonCreateCompany.Location = new Point(7, 407);
buttonCreateCompany.Margin = new Padding(3, 4, 3, 4);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(194, 23);
buttonCreateCompany.Size = new Size(222, 31);
buttonCreateCompany.TabIndex = 7;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
@ -160,16 +169,18 @@
panelStorage.Controls.Add(textBoxCollectionName);
panelStorage.Controls.Add(labelCollectionName);
panelStorage.Dock = DockStyle.Top;
panelStorage.Location = new Point(3, 19);
panelStorage.Location = new Point(3, 24);
panelStorage.Margin = new Padding(3, 4, 3, 4);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(200, 251);
panelStorage.Size = new Size(229, 335);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(3, 221);
buttonCollectionDel.Location = new Point(3, 295);
buttonCollectionDel.Margin = new Padding(3, 4, 3, 4);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(194, 23);
buttonCollectionDel.Size = new Size(222, 31);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
@ -178,17 +189,18 @@
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 15;
listBoxCollection.Location = new Point(3, 121);
listBoxCollection.Location = new Point(3, 161);
listBoxCollection.Margin = new Padding(3, 4, 3, 4);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(194, 94);
listBoxCollection.Size = new Size(221, 124);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(3, 82);
buttonCollectionAdd.Location = new Point(3, 109);
buttonCollectionAdd.Margin = new Padding(3, 4, 3, 4);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(194, 23);
buttonCollectionAdd.Size = new Size(222, 31);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
@ -197,9 +209,10 @@
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(106, 57);
radioButtonList.Location = new Point(121, 76);
radioButtonList.Margin = new Padding(3, 4, 3, 4);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(66, 19);
radioButtonList.Size = new Size(80, 24);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
@ -208,9 +221,10 @@
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(27, 57);
radioButtonMassive.Location = new Point(31, 76);
radioButtonMassive.Margin = new Padding(3, 4, 3, 4);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(67, 19);
radioButtonMassive.Size = new Size(82, 24);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
@ -218,17 +232,18 @@
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(3, 28);
textBoxCollectionName.Location = new Point(3, 37);
textBoxCollectionName.Margin = new Padding(3, 4, 3, 4);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(194, 23);
textBoxCollectionName.Size = new Size(221, 27);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(38, 10);
labelCollectionName.Location = new Point(43, 13);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(125, 15);
labelCollectionName.Size = new Size(158, 20);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
@ -238,27 +253,31 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" });
comboBoxSelectorCompany.Location = new Point(6, 276);
comboBoxSelectorCompany.Location = new Point(7, 368);
comboBoxSelectorCompany.Margin = new Padding(3, 4, 3, 4);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(194, 23);
comboBoxSelectorCompany.Size = new Size(221, 28);
comboBoxSelectorCompany.TabIndex = 0;
comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged;
//
// pictureBox
//
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 24);
pictureBox.Location = new Point(0, 30);
pictureBox.Margin = new Padding(3, 4, 3, 4);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(883, 583);
pictureBox.Size = new Size(855, 779);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(1089, 24);
menuStrip.Padding = new Padding(7, 3, 0, 3);
menuStrip.Size = new Size(1090, 30);
menuStrip.TabIndex = 2;
menuStrip.Text = "menuStrip";
//
@ -266,14 +285,14 @@
//
файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem });
файлToolStripMenuItem.Name = айлToolStripMenuItem";
файлToolStripMenuItem.Size = new Size(48, 20);
файлToolStripMenuItem.Size = new Size(59, 24);
файлToolStripMenuItem.Text = "Файл";
//
// saveToolStripMenuItem
//
saveToolStripMenuItem.Name = "saveToolStripMenuItem";
saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S;
saveToolStripMenuItem.Size = new Size(181, 22);
saveToolStripMenuItem.Size = new Size(227, 26);
saveToolStripMenuItem.Text = "Сохранение";
saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
@ -281,7 +300,7 @@
//
loadToolStripMenuItem.Name = "loadToolStripMenuItem";
loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L;
loadToolStripMenuItem.Size = new Size(181, 22);
loadToolStripMenuItem.Size = new Size(227, 26);
loadToolStripMenuItem.Text = "Загрузка";
loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -295,13 +314,14 @@
//
// FormAirplaneCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1089, 607);
ClientSize = new Size(1090, 809);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Margin = new Padding(3, 4, 3, 4);
Name = "FormAirplaneCollection";
Text = "Коллекция самолетов";
groupBoxTools.ResumeLayout(false);

View File

@ -1,5 +1,6 @@
using ProjectAirplaneWithRadar.CollectionGenericObjects;
using ProjectAirplaneWithRadar.Drawnings;
using ProjectAirplaneWithRadar.Exceptions;
namespace ProjectAirplaneWithRadar
{
@ -56,18 +57,23 @@ namespace ProjectAirplaneWithRadar
private void SetAirplane(DrawningAirplane airplane)
{
if (_company == null || airplane == null)
{
return;
}
try
{
if (_company + airplane != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
}
else
}
catch (CollectionOverflowException ex)
{
MessageBox.Show("Не удалось добавить объект");
MessageBox.Show(ex.Message);
}
catch (ObjectNotFoundException ex)
{
MessageBox.Show(ex.Message);
}
}
@ -89,14 +95,20 @@ namespace ProjectAirplaneWithRadar
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
try
{
if (_company - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _company.Show();
}
else
} catch (PositionOutOfCollectionException ex)
{
MessageBox.Show("Не удалось удалить объект");
MessageBox.Show(ex.Message);
} catch (ObjectNotFoundException ex)
{
MessageBox.Show(ex.Message);
}
}
@ -115,6 +127,8 @@ namespace ProjectAirplaneWithRadar
DrawningAirplane? plane = null;
int counter = 100;
while (plane == null)
{
try
{
plane = _company.GetRandomObject();
counter--;
@ -123,6 +137,15 @@ namespace ProjectAirplaneWithRadar
break;
}
}
catch (PositionOutOfCollectionException ex)
{
MessageBox.Show(ex.Message);
}
catch (ObjectNotFoundException ex)
{
MessageBox.Show(ex.Message);
}
}
if (plane == null)
{