Генерация ошибок

This commit is contained in:
Denis 2022-11-22 00:51:18 +04:00
parent bdfd25c4cb
commit 58f9a4f809
4 changed files with 90 additions and 23 deletions

View File

@ -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 AirplaneWithRadar
{
[Serializable]
internal class AirplaneNotFoundException : ApplicationException
{
public AirplaneNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
public AirplaneNotFoundException() : base() { }
public AirplaneNotFoundException(string message) : base(message) { }
public AirplaneNotFoundException(string message, Exception exception) : base(message, exception) { }
protected AirplaneNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}

View File

@ -84,6 +84,8 @@ namespace AirplaneWithRadar
} }
} }
private void AddAirplane(DrawningAirplane airplane) private void AddAirplane(DrawningAirplane airplane)
{
try
{ {
if (ListBoxMaps.SelectedIndex == -1) if (ListBoxMaps.SelectedIndex == -1)
{ {
@ -98,7 +100,15 @@ namespace AirplaneWithRadar
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
} }
}
catch (StorageOverflowException ex)
{
MessageBox.Show($"Ошибка хранилище переполнено: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
/// <summary> /// <summary>
/// Добавление объекта /// Добавление объекта
@ -131,7 +141,8 @@ namespace AirplaneWithRadar
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
try
{
if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? String.Empty] - pos != null) if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? String.Empty] - pos != null)
{ {
MessageBox.Show("Объект удалён"); MessageBox.Show("Объект удалён");
@ -142,6 +153,15 @@ namespace AirplaneWithRadar
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
catch (AirplaneNotFoundException ex)
{
MessageBox.Show($"Ошибка удаления: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
}
}
/// <summary> /// <summary>
/// Вывод набора /// Вывод набора
/// </summary> /// </summary>
@ -253,7 +273,7 @@ namespace AirplaneWithRadar
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"Не сохранилось:{ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }

View File

@ -26,19 +26,28 @@
// Добавление объекта в набор на конкретную позицию // Добавление объекта в набор на конкретную позицию
public int Insert(T airplane, int position) public int Insert(T airplane, int position)
{ {
if (position >= _maxCount || position < 0) return -1; if (Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
if (position < 0 || position > _maxCount) return -1;
_places.Insert(position, airplane); _places.Insert(position, airplane);
return position; return position;
} }
// Удаление объекта из набора с конкретной позиции // Удаление объекта из набора с конкретной позиции
public T Remove(int position) public T Remove(int position)
{ {
// проверка позиции if (position < Count || position > 0)
if (position >= _maxCount || position < 0) return null; {
// удаление объекта из массива, присовив элементу массива значение null if (_places.ElementAt(position) != null)
T temp = _places[position]; {
T ship = _places[position];
_places.RemoveAt(position); _places.RemoveAt(position);
return temp; return ship;
}
throw new AirplaneNotFoundException(position);
}
return null;
} }
// Получение объекта из набора по позиции // Получение объекта из набора по позиции
public T this[int position] public T this[int position]

View File

@ -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 AirplaneWithRadar
{
[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 context) : base(info, context) { }
}
}