From a7feb55e23f0c925c5e0acfceeedfb5980e7e37a Mon Sep 17 00:00:00 2001 From: shadowik Date: Mon, 28 Nov 2022 17:38:26 +0400 Subject: [PATCH] Exeptions --- .../DoubleDeckerBus/BusNotFoundException.cs | 19 +++++++++ .../DoubleDeckerBus/FormMapWithSetBuses.cs | 39 ++++++++++++------- .../DoubleDeckerBus/MapsCollection.cs | 11 ++---- .../DoubleDeckerBus/SetBusesGeneric.cs | 6 ++- .../StorageOverFlowException.cs | 19 +++++++++ 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/StorageOverFlowException.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.cs b/DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.cs new file mode 100644 index 0000000..3757dac --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.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 DoubleDeckerBus +{ + [Serializable] + internal class BusNotFoundException : ApplicationException + { + public BusNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public BusNotFoundException() : base() { } + public BusNotFoundException(string message) : base(message) { } + public BusNotFoundException(string message, Exception exception) : base(message, exception) { } + protected BusNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs index 9b404cb..f02e109 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs @@ -88,15 +88,27 @@ namespace DoubleDeckerBus 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 (BusNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); } + catch (Exception ex) + { + MessageBox.Show($"Неизветсная шибка: {ex.Message}"); + } + } private void ButtonShowStorage_Click(object sender, EventArgs e) @@ -183,13 +195,13 @@ namespace DoubleDeckerBus { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { + _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - else - { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + catch (Exception ex) { + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -198,13 +210,14 @@ namespace DoubleDeckerBus { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) - { + try { + _mapsCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } - else { - MessageBox.Show("Ошибка при загрузке", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + catch (Exception ex) + { + MessageBox.Show($"Ошибка при загрузке: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs index 5642947..c7e70e2 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs @@ -51,7 +51,7 @@ namespace DoubleDeckerBus stream.Write(info, 0, info.Length); } - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -65,14 +65,13 @@ namespace DoubleDeckerBus sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, 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 line; using (StreamReader sw = new(filename)) @@ -80,7 +79,7 @@ namespace DoubleDeckerBus line = sw.ReadLine(); if (line == null || !line.Contains("MapsCollection")) { - return false; + throw new Exception("Формат данных в файле не совпадает"); } _mapStorages.Clear(); @@ -102,8 +101,6 @@ namespace DoubleDeckerBus _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); line = sw.ReadLine(); } - - return true; } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs index 1da17a2..00927bc 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -28,9 +28,12 @@ namespace DoubleDeckerBus } public int Insert(T bus, int position) - { + { + // TODO проверка на _maxCount if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1; + // TODO проверка позиции + BusyPlaces++; _places.Insert(position, bus); return position; @@ -38,6 +41,7 @@ namespace DoubleDeckerBus public T Remove(int position) { + // TODO проверка позиции if (position < 0 || position >= _maxCount) return null; T savedBus = _places[position]; _places.RemoveAt(position); diff --git a/DoubleDeckerBus/DoubleDeckerBus/StorageOverFlowException.cs b/DoubleDeckerBus/DoubleDeckerBus/StorageOverFlowException.cs new file mode 100644 index 0000000..1e6d319 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/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 DoubleDeckerBus +{ + [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 exception) : base(message, exception) { } + protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +}