From e1c1dd5284fdb063ca5a4bc28cd4f188626e8024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Sat, 26 Nov 2022 14:07:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirBomber/AirBomberNotFoundException.cs | 19 ++++++++++ .../AirBomber/FormMapWithSetAirBomber.cs | 37 +++++++++++++------ AirBomber/AirBomber/MapsCollection.cs | 10 ++--- AirBomber/AirBomber/SetAirBomberGeneric.cs | 10 ++++- .../AirBomber/StorageOverflowException.cs | 19 ++++++++++ 5 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 AirBomber/AirBomber/AirBomberNotFoundException.cs create mode 100644 AirBomber/AirBomber/StorageOverflowException.cs diff --git a/AirBomber/AirBomber/AirBomberNotFoundException.cs b/AirBomber/AirBomber/AirBomberNotFoundException.cs new file mode 100644 index 0000000..7e674a9 --- /dev/null +++ b/AirBomber/AirBomber/AirBomberNotFoundException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + [Serializable] + internal class AirBomberNotFoundException : ApplicationException + { + public AirBomberNotFoundException() : base() { } + public AirBomberNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public AirBomberNotFoundException(string message) : base(message) { } + public AirBomberNotFoundException(string message, Exception exception) : base (message, exception) { } + protected AirBomberNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs index 2aed3ef..5f11999 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs @@ -93,14 +93,25 @@ namespace AirBomber return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } } - else + catch(AirBomberNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } } @@ -187,13 +198,14 @@ namespace AirBomber { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { + _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch(Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); ; ; } } } @@ -202,15 +214,16 @@ namespace AirBomber { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { - MessageBox.Show("Загрузка прошла успешно", "Результат", + _mapsCollection.LoadData(openFileDialog.FileName); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } - else + catch(Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/AirBomber/AirBomber/MapsCollection.cs b/AirBomber/AirBomber/MapsCollection.cs index f5868cd..94bef9b 100644 --- a/AirBomber/AirBomber/MapsCollection.cs +++ b/AirBomber/AirBomber/MapsCollection.cs @@ -45,7 +45,7 @@ namespace AirBomber } } - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -60,14 +60,13 @@ namespace AirBomber separatorData)}{Environment.NewLine}"); } } - return true; } - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найден"); } string bufferTextFromFile = ""; using (StreamReader sr = new(filename)) @@ -80,7 +79,7 @@ namespace AirBomber { if (!str.Contains("MapsCollection")) { - return false; + throw new Exception("Формат данных в файлен не правильный"); } else { @@ -111,7 +110,6 @@ namespace AirBomber } } } - return true; } } } diff --git a/AirBomber/AirBomber/SetAirBomberGeneric.cs b/AirBomber/AirBomber/SetAirBomberGeneric.cs index db6bec4..0651605 100644 --- a/AirBomber/AirBomber/SetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/SetAirBomberGeneric.cs @@ -30,7 +30,7 @@ namespace AirBomber { if (position < 0 || position > _maxCount) { - return -1; + throw new StorageOverflowException(_maxCount); } _places.Insert(position, airBomber); return position; @@ -40,10 +40,16 @@ namespace AirBomber { if (position < 0 || position > _maxCount) { - return null; + throw new StorageOverflowException(_maxCount); } + T elem = _places[position]; _places[position] = null; + + if (elem == null) + { + throw new AirBomberNotFoundException(position); + } return elem; } diff --git a/AirBomber/AirBomber/StorageOverflowException.cs b/AirBomber/AirBomber/StorageOverflowException.cs new file mode 100644 index 0000000..a619a5a --- /dev/null +++ b/AirBomber/AirBomber/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + [Serializable] + internal class StorageOverflowException : ApplicationException + { + public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { } + public StorageOverflowException() : base() { } + public StorageOverflowException(string message) : base(message) { } + public StorageOverflowException(string message, Exception innerException) : base(message, innerException) { } + protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +}