Använd vänsterpil och högerpil för att navigera

Eller Ctrl+P för att skriva ut till PDF (eller på papper)

Föreläsning 13

Felhantering och filhantering 😉

Felhantering

Fånga kastade fel

Ofångat fel

  1. static void Main(string[] args) {
  2. FirstMethod();
  3. SecondMethod();
  4. }
  5. static void FirstMethod() {
  6. Console.WriteLine("FirstMethod before throw");
  7. throw new ArgumentException("FirstMethod error");
  8. Console.WriteLine("FirstMethod after throw");
  9. }
  10. static void SecondMethod() {
  11. Console.WriteLine("SecondMethod");
  12. }

Fångat fel

  1. static void Main(string[] args) {
  2. try {
  3. FirstMethod();
  4. }
  5. catch(ArgumentException) {
  6. }
  7. SecondMethod();
  8. }
  9. static void FirstMethod() {
  10. Console.WriteLine("FirstMethod before throw");
  11. throw new ArgumentException("FirstMethod error");
  12. Console.WriteLine("FirstMethod after throw");
  13. }
  14. static void SecondMethod() {
  15. Console.WriteLine("SecondMethod");
  16. }

Fångat fel med meddelande

  1. static void Main(string[] args) {
  2. try {
  3. FirstMethod();
  4. }
  5. catch(ArgumentException e) {
  6. Console.WriteLine(e.Message);
  7. }
  8. SecondMethod();
  9. }
  10. static void FirstMethod() {
  11. Console.WriteLine("FirstMethod before throw");
  12. throw new ArgumentException("FirstMethod error");
  13. Console.WriteLine("FirstMethod after throw");
  14. }

Vad händer här?

  1. int? number = null;
  2. while (number == null) {
  3. Console.Write("Please enter a number: ");
  4. number = int.Parse(Console.ReadLine());
  5. }
  6. Console.WriteLine("You entered: " + number);

Hantera felinmatning

  1. int? number = null;
  2. while (number == null) {
  3. try {
  4. Console.Write("Please enter a number: ");
  5. number = int.Parse(Console.ReadLine());
  6. }
  7. catch(FormatException) {
  8. Console.WriteLine("You did not enter a number");
  9. }
  10. }
  11. Console.WriteLine("You entered: " + number);

Hantera alla tänkbara fel

  1. int? number = null;
  2. while (number == null) {
  3. try {
  4. Console.Write("Please enter a number: ");
  5. number = int.Parse(Console.ReadLine());
  6. }
  7. catch {
  8. Console.WriteLine("You did not enter a number");
  9. }
  10. }
  11. Console.WriteLine("You entered: " + number);

Fel flera nivåer in

  1. static void Main(string[] args) {
  2. try {
  3. FirstMethod();
  4. }
  5. catch(ArgumentException) {
  6. Console.WriteLine("Error caught");
  7. }
  8. Console.WriteLine("After catch");
  9. }
  10. static void FirstMethod() {
  11. Console.WriteLine("FirstMethod before SecondMethod");
  12. SecondMethod();
  13. Console.WriteLine("FirstMethod after SecondMethod");
  14. }
  15. static void SecondMethod() {
  16. Console.WriteLine("SecondMethod before error");
  17. throw new ArgumentException("SecondMethod error");
  18. Console.WriteLine("SecondMethod after error");
  19. }

Tvetydiga fel

  1. try {
  2. DisplayMessage(font: "Arial", message: "Hello!");
  3. }
  4. catch(ArgumentException) {
  5. // Which argument was wrong?
  6. }

Vad vi skulle vilja ha

  1. try {
  2. DisplayMessage(font: "Arial", message: "Hello!");
  3. }
  4. catch(MissingFontException) {
  5. ...
  6. }
  7. catch(TooLongMessageException) {
  8. ...
  9. }

Egna felklasser

  1. class MissingFontException : Exception {
  2. ...
  3. }
  4. class TooLongMessageException : Exception {
  5. ...
  6. }
  7. static void DisplayMessage(string font, string message) {
  8. ...
  9. throw new MissingFontException();
  10. ...
  11. throw new TooLongMessageException();
  12. ...
  13. }

Filhantering

Spara data till och läs data från hårddisken

Exempel.txt

  1. This is some text
  2. This is some text on a different line
  3. Some numbers: 1 2 3 4 5 6 7 8 9 0

Exempel.txt med synliga nyrader

  1. This is some text\n
  2. This is some text on a different line\n
  3. Some numbers: 1 2 3 4 5 6 7 8 9 0

Vad är datatypen på en fil?

Läsa en fil

  1. using System.IO;
  2. ...
  3. static void Main(string[] args) {
  4. string path = @"C:\Windows\System32\drivers\etc";
  5. string text = File.ReadAllText(path);
  6. string lines[] = File.ReadAllLines(path);
  7. }

Läsa en fil via relativ sökväg

  1. using System.IO;
  2. ...
  3. static void Main(string[] args) {
  4. string path = "Test.txt";
  5. string text = File.ReadAllText(path);
  6. }

"Be om ursäkt"

  1. using System.IO;
  2. ...
  3. static void Main(string[] args) {
  4. string path = "Test.txt";
  5. try {
  6. string text = File.ReadAllText(path);
  7. }
  8. catch (FileNotFoundException) {
  9. Console.WriteLine("Please create Test.txt");
  10. }
  11. }

"Be om tillåtelse"

  1. using System.IO;
  2. ...
  3. static void Main(string[] args) {
  4. string path = "Test.txt";
  5. if (File.Exists(path))
  6. string text = File.ReadAllText(path);
  7. }
  8. else {
  9. Console.WriteLine("Please create Test.txt");
  10. }
  11. }

Läsa och skriva

  1. using System.IO;
  2. ...
  3. static void Main(string[] args) {
  4. string path = "Test.txt";
  5. File.WriteAllText(path, "These are the contents");
  6. string text = File.ReadAllText(path);
  7. Console.WriteLine(text);
  8. }

Exempel på användningsområde

  1. using System.IO;
  2. ...
  3. static void Main(string[] args) {
  4. Console.Write("Enter discount code: ");
  5. string code = Console.ReadLine();
  6. string[] validCodes = File.ReadAllLines("Codes.txt");
  7. if (validCodes.Contains(code)) {
  8. Console.WriteLine("You will receive a discount!");
  9. }
  10. else {
  11. Console.WriteLine(code + " is not valid.");
  12. }
  13. }

Mer avancerat exempel

  1. using System.IO;
  2. ...
  3. static void Main(string[] args) {
  4. string[] lines = File.ReadAllLines("People.txt");
  5. List<Person> people = new List<Person>();
  6. foreach (string line in lines) {
  7. string[] parts = line.Split(',');
  8. string name = parts[0];
  9. int age = int.Parse(parts[1]);
  10. people.Add(new Person {
  11. Name = name,
  12. Age = age
  13. });
  14. }
  15. }