c# - Using try/catch to display errors depending on input -
I have an extraordinary assignment, using try / catch
to show many names Including the number entered (using an array) If the number entered is too large then it can still display the name, but it also has to give a faulty error. If a word or something is used in such a way it needs to give a format error.
My code works fairly well because it can display an error outside of a range, but when I put a word, I get the format error.
I would also like to know if there is a possibility of an error, if the number is less than 5 (in such cases where only 5 are acceptable).
Here is my code:
class program {static zero main (string [] args) {string [] names = new string [5] {"Mary" , "John", "Tim", "Matt", "Jeff"}; Read String = console Readline (); Intel Apple; Try {int.TryParse (Read, Appel out); {Int a = 0; a & lt; apel; a ++) for {console}. Vidline (name [a]); }} Catch (formatException E) {console.lightline ("This is a format error: {0}", E. Message); } Catch (overflowException E) {console.lightline ("{0} is out of range 5. Error message: {1}", e.Message); } Hold (exception e) {Console.WriteLine ("error error message out of bounds: {0}", e. Message); } Console. Readline (); }}
int.TryParse (read, apply);
This code will not throw any exception, it will either return the truth (if the parsing goes wrong). If you intend to use exceptions:
Comments
Post a Comment