Генерация исключений
This commit is contained in:
19
Boats/Boats/BoatNotFoundException.cs
Normal file
19
Boats/Boats/BoatNotFoundException.cs
Normal 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 Boats
|
||||||
|
{
|
||||||
|
[Serializable]
|
||||||
|
internal class BoatNotFoundException : ApplicationException
|
||||||
|
{
|
||||||
|
public BoatNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
|
||||||
|
public BoatNotFoundException() : base() { }
|
||||||
|
public BoatNotFoundException(string message) : base(message) { }
|
||||||
|
public BoatNotFoundException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
protected BoatNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -124,16 +124,27 @@ namespace Boats
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||||||
pos -= 1;
|
try
|
||||||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
|
|
||||||
{
|
{
|
||||||
MessageBox.Show("Объект удален");
|
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
|
||||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
{
|
||||||
|
MessageBox.Show("Объект удален");
|
||||||
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox.Show("Не удалось удалить объект");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (BoatNotFoundException ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось удалить объект");
|
MessageBox.Show($"Ошибка удаления: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вывод набора
|
/// Вывод набора
|
||||||
@@ -270,13 +281,14 @@ namespace Boats
|
|||||||
{
|
{
|
||||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_mapsCollection.SaveData(saveFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_mapsCollection.SaveData(saveFileDialog.FileName);
|
||||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -289,15 +301,16 @@ namespace Boats
|
|||||||
{
|
{
|
||||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_mapsCollection.LoadData(openFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_mapsCollection.LoadData(openFileDialog.FileName);
|
||||||
ReloadMaps();
|
ReloadMaps();
|
||||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||||
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ namespace Boats
|
|||||||
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
Bitmap bmp = new(_pictureWidth, _pictureHeight);
|
||||||
Graphics g = Graphics.FromImage(bmp);
|
Graphics g = Graphics.FromImage(bmp);
|
||||||
DrawBackground(g);
|
DrawBackground(g);
|
||||||
|
Shaking();
|
||||||
DrawBoats(g);
|
DrawBoats(g);
|
||||||
return bmp;
|
return bmp;
|
||||||
}
|
}
|
||||||
@@ -135,8 +136,8 @@ namespace Boats
|
|||||||
var boat = _setBoats[j];
|
var boat = _setBoats[j];
|
||||||
if (boat != null)
|
if (boat != null)
|
||||||
{
|
{
|
||||||
|
boat = _setBoats.Remove(j);
|
||||||
_setBoats.Insert(boat, i);
|
_setBoats.Insert(boat, i);
|
||||||
_setBoats.Remove(j);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ namespace Boats
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">Путь и имя файла</param>
|
/// <param name="filename">Путь и имя файла</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public bool SaveData(string filename)
|
public void SaveData(string filename)
|
||||||
{
|
{
|
||||||
if (File.Exists(filename))
|
if (File.Exists(filename))
|
||||||
{
|
{
|
||||||
@@ -107,18 +107,17 @@ namespace Boats
|
|||||||
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
|
sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Загрузка нформации по лодкам в гавани из файла
|
/// Загрузка нформации по лодкам в гавани из файла
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename"></param>
|
/// <param name="filename"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public bool LoadData(string filename)
|
public void LoadData(string filename)
|
||||||
{
|
{
|
||||||
if (!File.Exists(filename))
|
if (!File.Exists(filename))
|
||||||
{
|
{
|
||||||
return false;
|
throw new Exception("Файл не найден");
|
||||||
}
|
}
|
||||||
using (StreamReader sr = File.OpenText(filename))
|
using (StreamReader sr = File.OpenText(filename))
|
||||||
{
|
{
|
||||||
@@ -126,7 +125,7 @@ namespace Boats
|
|||||||
if (currentLine == null || !currentLine.Contains("MapsCollection"))
|
if (currentLine == null || !currentLine.Contains("MapsCollection"))
|
||||||
{
|
{
|
||||||
//если нет такой записи, то это не те данные
|
//если нет такой записи, то это не те данные
|
||||||
return false;
|
throw new Exception("Формат данных в файле не правильный");
|
||||||
}
|
}
|
||||||
//очищаем записи
|
//очищаем записи
|
||||||
_mapStorages.Clear();
|
_mapStorages.Clear();
|
||||||
@@ -156,7 +155,6 @@ namespace Boats
|
|||||||
currentLine = sr.ReadLine();
|
currentLine = sr.ReadLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,9 @@ namespace Boats
|
|||||||
public int Insert(T boat)
|
public int Insert(T boat)
|
||||||
{
|
{
|
||||||
// Проверка на _maxCount
|
// Проверка на _maxCount
|
||||||
|
// Если достигли максимального значения - выбрасываем исключение
|
||||||
if (Count == _maxCount)
|
if (Count == _maxCount)
|
||||||
return -1;
|
throw new StorageOverflowException(_maxCount);
|
||||||
// Вставка в начало набора
|
// Вставка в начало набора
|
||||||
return Insert(boat, 0);
|
return Insert(boat, 0);
|
||||||
}
|
}
|
||||||
@@ -69,9 +70,11 @@ namespace Boats
|
|||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
// Проверка позиции
|
// Проверка позиции
|
||||||
if (Count == 0 || position < 0 || position >= _maxCount)
|
// Если позиция уже пустая выбрасываем исключение
|
||||||
return null;
|
if (Count == 0 || position < 0 || position >= _maxCount || _places[position] == null)
|
||||||
|
throw new BoatNotFoundException(position);
|
||||||
T boat = _places[position];
|
T boat = _places[position];
|
||||||
|
|
||||||
_places[position] = null;
|
_places[position] = null;
|
||||||
return boat;
|
return boat;
|
||||||
}
|
}
|
||||||
|
|||||||
19
Boats/Boats/StorageOverflowException.cs
Normal file
19
Boats/Boats/StorageOverflowException.cs
Normal 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 Boats
|
||||||
|
{
|
||||||
|
[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) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user