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

This commit is contained in:
Мельников Игорь 2022-11-23 21:04:33 +04:00
parent 76b7233095
commit 8ed442d581
6 changed files with 90 additions and 41 deletions

View File

@ -70,7 +70,7 @@
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddCar); this.groupBoxTools.Controls.Add(this.buttonAddCar);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(950, 24); this.groupBoxTools.Location = new System.Drawing.Point(462, 24);
this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(223, 652); this.groupBoxTools.Size = new System.Drawing.Size(223, 652);
this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabIndex = 0;
@ -243,7 +243,7 @@
this.pictureBoxLocomotives.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxLocomotives.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxLocomotives.Location = new System.Drawing.Point(0, 24); this.pictureBoxLocomotives.Location = new System.Drawing.Point(0, 24);
this.pictureBoxLocomotives.Name = "pictureBoxLocomotives"; this.pictureBoxLocomotives.Name = "pictureBoxLocomotives";
this.pictureBoxLocomotives.Size = new System.Drawing.Size(950, 652); this.pictureBoxLocomotives.Size = new System.Drawing.Size(462, 652);
this.pictureBoxLocomotives.TabIndex = 1; this.pictureBoxLocomotives.TabIndex = 1;
this.pictureBoxLocomotives.TabStop = false; this.pictureBoxLocomotives.TabStop = false;
// //
@ -253,7 +253,7 @@
this.FileToolStripMenuItem}); this.FileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1173, 24); this.menuStrip1.Size = new System.Drawing.Size(685, 24);
this.menuStrip1.TabIndex = 2; this.menuStrip1.TabIndex = 2;
this.menuStrip1.Text = "menuStrip"; this.menuStrip1.Text = "menuStrip";
// //
@ -269,14 +269,14 @@
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
// //
@ -292,7 +292,7 @@
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1173, 676); this.ClientSize = new System.Drawing.Size(685, 676);
this.Controls.Add(this.pictureBoxLocomotives); this.Controls.Add(this.pictureBoxLocomotives);
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip1); this.Controls.Add(this.menuStrip1);

View File

@ -111,19 +111,30 @@
private void ButtonAddLocomotive_Click(object sender, EventArgs e) private void ButtonAddLocomotive_Click(object sender, EventArgs e)
{ {
FormLocomotiveConfig formLocomotiveConfig = new(); FormLocomotiveConfig formLocomotiveConfig = new();
formLocomotiveConfig.AddEvent(new (AddLocomotive)); formLocomotiveConfig.AddEvent(new(AddLocomotive));
formLocomotiveConfig.Show(); formLocomotiveConfig.Show();
} }
private void AddLocomotive(DrawningLocomotive locomotive) private void AddLocomotive(DrawningLocomotive locomotive)
{ {
if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive)) > -1) try
{ {
MessageBox.Show("Объект добавлен"); if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive)) > -1)
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); {
MessageBox.Show("Объект добавлен");
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
} }
else catch (StorageOverflowException ex)
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show($"Ошибка добавления: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
} }
} }
/// <summary> /// <summary>
@ -146,16 +157,26 @@
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) > -1) try
{ {
MessageBox.Show("Объект удален"); if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) > -1)
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); {
MessageBox.Show("Объект удален");
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
} }
else catch (LocomotiveNotFoundException ex)
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show($"Ошибка удаления: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
} }
} }
/// <summary> /// <summary>
/// Вывод набора /// Вывод набора
@ -223,15 +244,16 @@
{ {
if (saveFileDialog.ShowDialog() == DialogResult.OK) if (saveFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_mapsCollection.SaveData(saveFileDialog.FileName)) try
{ {
MessageBox.Show("Сохранение прошло успешно", "Результат", _mapsCollection.SaveData(saveFileDialog.FileName);
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Сохранение прошло успешно", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
else catch(Exception ex)
{ {
MessageBox.Show("Не сохранилось", "Результат", MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }
@ -244,16 +266,17 @@
{ {
if (openFileDialog.ShowDialog() == DialogResult.OK) if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_mapsCollection.LoadData(openFileDialog.FileName)) try
{ {
MessageBox.Show("Загрузка прошла успешно", "Результат", _mapsCollection.LoadData(openFileDialog.FileName);
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Загрузка прошла успешно", "Результат",
ReloadMaps(); MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadMaps();
} }
else catch (Exception ex)
{ {
MessageBox.Show("Не удалось загрузить файл", "Результат", MessageBox.Show($"Не удалось загрузить файл: {ex.Message}", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }

View File

@ -0,0 +1,14 @@
using System.Runtime.Serialization;
namespace Locomotives
{
[Serializable]
internal class LocomotiveNotFoundException : ApplicationException
{
public LocomotiveNotFoundException(int i) : base($"Не наден объект по позиции {i}") { }
public LocomotiveNotFoundException() : base() { }
public LocomotiveNotFoundException(string message) : base(message) { }
public LocomotiveNotFoundException(string message, Exception Exception) : base(message, Exception) { }
protected LocomotiveNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}

View File

@ -71,7 +71,7 @@ namespace Locomotives
return _mapStorages[ind]; return _mapStorages[ind];
} }
} }
public bool SaveData(string filename) public void SaveData(string filename)
{ {
if (File.Exists(filename)) if (File.Exists(filename))
{ {
@ -86,13 +86,12 @@ namespace Locomotives
} }
sw.Close(); sw.Close();
} }
return true;
} }
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 = new(filename)) using (StreamReader sr = new(filename))
{ {
@ -100,7 +99,7 @@ namespace Locomotives
if (firstStr == null || !firstStr.Contains("MapsCollection")) if (firstStr == null || !firstStr.Contains("MapsCollection"))
{ {
//если нет такой записи, то это не те данные //если нет такой записи, то это не те данные
return false; throw new Exception("Формат данных в файле неправильный");
} }
string? currentString; string? currentString;
while ((currentString = sr.ReadLine()) != null) while ((currentString = sr.ReadLine()) != null)
@ -123,7 +122,6 @@ namespace Locomotives
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
} }
sr.Close(); sr.Close();
return true;
} }
} }
} }

View File

@ -43,7 +43,7 @@
_places.Insert(0, locomotive); _places.Insert(0, locomotive);
return 0; return 0;
} }
return -1; throw new StorageOverflowException(_places.Count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -62,7 +62,7 @@
_places.Insert(position, locomotive); _places.Insert(position, locomotive);
return position; return position;
} }
return -1; throw new StorageOverflowException(_places.Count);
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -71,9 +71,9 @@
/// <returns></returns> /// <returns></returns>
public int Remove(int position) public int Remove(int position)
{ {
if (position > Count || _places[position] == null) if (position >= Count || _places[position] == null)
{ {
return -1; throw new LocomotiveNotFoundException(position);
} }
_places.RemoveAt(position); _places.RemoveAt(position);
return position; return position;

View File

@ -0,0 +1,14 @@
using System.Runtime.Serialization;
namespace Locomotives
{
[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) { }
}
}