Допустима ли пустая попытка и перехват IndexOutOfBoundsException в конце цикла while операторов if в C#?C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Допустима ли пустая попытка и перехват IndexOutOfBoundsException в конце цикла while операторов if в C#?

Сообщение Anonymous »


I'm finishing an unfinished C# WinForms project, which is a save game editor for an unspecified game. I noticed that character portraits and icons' IDs have a valid range of 1 to 905, but there was no code to verify that the character ID is within that range, so I opted to fix it. I have an in-progress solution to this, with the code for it (with a lot of function names partially redacted) at the end of this post, and wanted to know if it would work. Visual Studio says there are no formatting errors, but I know that alone will not quite be enough. I cannot compile to test because I am not quite at a stage where the code is compilable, with a lot of missing WinForms data. If the specific exception that I placed would not be appropriate, what exception would best fit? If a while loop should not be used in this way, what method should I use that doesn't require rewriting the entire code block from scratch? Here is the relevant code I have so far:

void getImage(int slot) { short ID = 0; if (slot == 1) { try { ID = convertStringtoID(comboBoxSlot1ID.Text); } catch (System.FormatException) { ID = 0; } while (ID > 0 && ID < 906) { if (ID < 10) { pictureBoxSlot1Portrait.Image = (Image)Properties.Resources.ResourceManager.GetObject("00" + ID); pictureBoxSlot1Icon.Image = (Image)Properties.Resources.ResourceManager.GetObject("icon00" + ID); } else if (ID > 9 && ID < 100) { pictureBoxSlot1Portrait.Image = (Image)Properties.Resources.ResourceManager.GetObject("0" + ID); pictureBoxSlot1Icon.Image = (Image)Properties.Resources.ResourceManager.GetObject("icon0" + ID); } else if (ID > 99 && ID < 906) { pictureBoxSlot1Portrait.Image = (Image)Properties.Resources.ResourceManager.GetObject("" + ID); pictureBoxSlot1Icon.Image = (Image)Properties.Resources.ResourceManager.GetObject("icon" + ID); } else { try { } catch (System.IndexOutOfRangeException) { ID = 0; } } } } } I have not yet been able to try or test anything yet; this is merely a question regarding the valid use of C# code. I also saw just 1 question that was similar, but not the same, as my question in the 'possibly related questions' section before posting this, although it did partially indicate that what I am doing may be okay. I am expecting the code to throw IndexOutOfBoundsException if ID is greater than 905, and read the proper image file if not.

Edit: Okay, I have revised the code significantly, using feedback posted here. I have ended up with the following code, which shows no errors in Visual Studio, and despite others saying 'it is improper to use exceptions to control flow', works for my use case. Also, I am technically not using them to control anything, just that I want an exception window to pop up for anyone who has a save file with a character ID greater than 905, so that they know that their save file is likely corrupted. I want the program to still be able to continue operation afterward, and not have this error be fatal. Here is the revised code:
void getImage(int slot) { short ID = 0; if (slot == 1) { try { ID = convertStringtoID(comboBoxSlot1ID.Text); } catch (System.FormatException) { ID = 0; } if (ID < 10) { pictureBoxSlot1Portrait.Image = (Image)Properties.Resources.ResourceManager.GetObject("00" + ID); pictureBoxSlot1Icon.Image = (Image)Properties.Resources.ResourceManager.GetObject("icon00" + ID); } else if (ID > 9 && ID < 100) { pictureBoxSlot1Portrait.Image = (Image)Properties.Resources.ResourceManager.GetObject("0" + ID); pictureBoxSlot1Icon.Image = (Image)Properties.Resources.ResourceManager.GetObject("icon0" + ID); } else if (ID > 99 && ID < 906) { pictureBoxSlot1Portrait.Image = (Image)Properties.Resources.ResourceManager.GetObject("" + ID); pictureBoxSlot1Icon.Image = (Image)Properties.Resources.ResourceManager.GetObject("icon" + ID); } else if (ID > 905) { throw(System.InvalidOperationException); ID = 0; continue; } } }

Источник: https://stackoverflow.com/questions/780 ... -a-while-l
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Допустима ли пустая попытка и перехват IndexOutOfBoundsException в конце цикла while операторов if в C#?
    Anonymous » » в форуме C#
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Перехват (java.lang.IndexOutOfBoundsException)
    Anonymous » » в форуме JAVA
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous
  • Пустая/пустая (привязка) страница категории Magento
    Anonymous » » в форуме Php
    0 Ответы
    32 Просмотры
    Последнее сообщение Anonymous
  • Допустима ли Noop JVM в рамках модели памяти Java?
    Anonymous » » в форуме JAVA
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Почему перегрузка операторов == и <=> необходима для перегрузки всех операторов сравнения? [дубликат]
    Anonymous » » в форуме C++
    0 Ответы
    77 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»