Генерация ошибок
This commit is contained in:
parent
b60f1e88fe
commit
56b1a724c3
19
DoubleDeckerBus/DoubleDeckerBus/BusNotFoundExeption.cs
Normal file
19
DoubleDeckerBus/DoubleDeckerBus/BusNotFoundExeption.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 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) { }
|
||||
}
|
||||
}
|
@ -86,15 +86,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)
|
||||
@ -181,13 +193,14 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ namespace DoubleDeckerBus
|
||||
}
|
||||
|
||||
|
||||
public bool SaveData(string filename)
|
||||
public void SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
@ -62,14 +62,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 FileNotFoundException("Файл не найден");
|
||||
}
|
||||
string line;
|
||||
using (StreamReader sw = new(filename))
|
||||
@ -77,7 +76,7 @@ namespace DoubleDeckerBus
|
||||
line = sw.ReadLine();
|
||||
if (line == null || !line.Contains("MapsCollection"))
|
||||
{
|
||||
return false;
|
||||
throw new FileFormatException("Формат данных в файле не совпадает");
|
||||
}
|
||||
|
||||
_mapStorages.Clear();
|
||||
@ -99,8 +98,6 @@ namespace DoubleDeckerBus
|
||||
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
|
||||
line = sw.ReadLine();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,10 @@ namespace DoubleDeckerBus
|
||||
/// <returns></returns>
|
||||
public int Insert(T bus, int position)
|
||||
{
|
||||
if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1;
|
||||
if (position < 0 || position >= _maxCount)
|
||||
{
|
||||
throw new BusNotFoundException("Место указано неверно");
|
||||
}
|
||||
|
||||
BusyPlaces++;
|
||||
_places.Insert(position, bus);
|
||||
@ -58,7 +61,10 @@ namespace DoubleDeckerBus
|
||||
/// <returns></returns>
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= _maxCount) return null;
|
||||
if (position < 0 || position >= _maxCount)
|
||||
{
|
||||
throw new BusNotFoundException(position);
|
||||
}
|
||||
T savedBus = _places[position];
|
||||
_places.RemoveAt(position);
|
||||
return savedBus;
|
||||
|
19
DoubleDeckerBus/DoubleDeckerBus/StorageOverflowException.cs
Normal file
19
DoubleDeckerBus/DoubleDeckerBus/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 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) { }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user