diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs index ee87e04..b542f9a 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs @@ -127,15 +127,22 @@ namespace WarmlyShip 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 + catch(ShipNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message} "); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message} "); } } @@ -188,13 +195,14 @@ namespace WarmlyShip { 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); } } } @@ -203,14 +211,15 @@ namespace WarmlyShip { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { + _mapsCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } - else + catch (Exception ex) { - MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 503080b..966ec7b 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -50,7 +50,7 @@ namespace WarmlyShip } } - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -65,14 +65,13 @@ namespace WarmlyShip 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("Файл не найден"); } using (StreamReader sr = new(filename)) { @@ -84,7 +83,7 @@ namespace WarmlyShip { if (!bufferStr.Contains("MapsCollection")) { - return false; + throw new Exception("Формат данных в файле не правильный"); } else { @@ -113,7 +112,6 @@ namespace WarmlyShip } } } - return true; } } } diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs index 7d336d8..c768070 100644 --- a/WarmlyShip/WarmlyShip/SetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -27,13 +27,14 @@ namespace WarmlyShip public int Insert(T ship, int position) { - if (position < 0 || position > Count || Count == _maxCount) return -1; + if (position < 0 || position > Count || Count == _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(position, ship); return position; } public T Remove(int position) { + if (position >= _maxCount || position >= _places.Count) throw new ShipNotFoundException(position); T DelElement = _places[position]; _places[position] = null; return DelElement; diff --git a/WarmlyShip/WarmlyShip/ShipNotFoundException.cs b/WarmlyShip/WarmlyShip/ShipNotFoundException.cs new file mode 100644 index 0000000..528b699 --- /dev/null +++ b/WarmlyShip/WarmlyShip/ShipNotFoundException.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 WarmlyShip +{ + [Serializable] + internal class ShipNotFoundException : Exception + { + public ShipNotFoundException(int i) : base($"Не найден объект по позиции { i}") { } + public ShipNotFoundException() : base() { } + public ShipNotFoundException(string message) : base(message) { } + public ShipNotFoundException(string message, Exception exception) : base(message, exception) { } + protected ShipNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/WarmlyShip/WarmlyShip/StorageOverflowException.cs b/WarmlyShip/WarmlyShip/StorageOverflowException.cs new file mode 100644 index 0000000..e6c922a --- /dev/null +++ b/WarmlyShip/WarmlyShip/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 WarmlyShip +{ + [Serializable] + internal class StorageOverflowException : Exception + { + 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) { } + } +}