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

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.buttonAddCar);
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.Size = new System.Drawing.Size(223, 652);
this.groupBoxTools.TabIndex = 0;
@ -243,7 +243,7 @@
this.pictureBoxLocomotives.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxLocomotives.Location = new System.Drawing.Point(0, 24);
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.TabStop = false;
//
@ -253,7 +253,7 @@
this.FileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
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.Text = "menuStrip";
//
@ -269,14 +269,14 @@
// 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.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// 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.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -292,7 +292,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
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.groupBoxTools);
this.Controls.Add(this.menuStrip1);

View File

@ -111,10 +111,12 @@
private void ButtonAddLocomotive_Click(object sender, EventArgs e)
{
FormLocomotiveConfig formLocomotiveConfig = new();
formLocomotiveConfig.AddEvent(new (AddLocomotive));
formLocomotiveConfig.AddEvent(new(AddLocomotive));
formLocomotiveConfig.Show();
}
private void AddLocomotive(DrawningLocomotive locomotive)
{
try
{
if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive)) > -1)
{
@ -126,6 +128,15 @@
MessageBox.Show("Не удалось добавить объект");
}
}
catch (StorageOverflowException ex)
{
MessageBox.Show($"Ошибка добавления: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
}
}
/// <summary>
/// Удаление объекта
/// </summary>
@ -146,6 +157,8 @@
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
try
{
if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) > -1)
{
MessageBox.Show("Объект удален");
@ -155,7 +168,15 @@
{
MessageBox.Show("Не удалось удалить объект");
}
}
catch (LocomotiveNotFoundException ex)
{
MessageBox.Show($"Ошибка удаления: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
}
}
/// <summary>
/// Вывод набора
@ -223,14 +244,15 @@
{
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("Не сохранилось", "Результат",
MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
@ -244,15 +266,16 @@
{
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("Не удалось загрузить файл", "Результат",
MessageBox.Show($"Не удалось загрузить файл: {ex.Message}", "Результат",
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];
}
}
public bool SaveData(string filename)
public void SaveData(string filename)
{
if (File.Exists(filename))
{
@ -86,13 +86,12 @@ namespace Locomotives
}
sw.Close();
}
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))
{
@ -100,7 +99,7 @@ namespace Locomotives
if (firstStr == null || !firstStr.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
throw new Exception("Формат данных в файле неправильный");
}
string? currentString;
while ((currentString = sr.ReadLine()) != null)
@ -123,7 +122,6 @@ namespace Locomotives
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
sr.Close();
return true;
}
}
}

View File

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