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
- static void Main(string[] args) {
- FirstMethod();
- SecondMethod();
- }
- static void FirstMethod() {
- Console.WriteLine("FirstMethod before throw");
- throw new ArgumentException("FirstMethod error");
- Console.WriteLine("FirstMethod after throw");
- }
- static void SecondMethod() {
- Console.WriteLine("SecondMethod");
- }
Fångat fel
- static void Main(string[] args) {
- try {
- FirstMethod();
- }
- catch(ArgumentException) {
- }
- SecondMethod();
- }
- static void FirstMethod() {
- Console.WriteLine("FirstMethod before throw");
- throw new ArgumentException("FirstMethod error");
- Console.WriteLine("FirstMethod after throw");
- }
- static void SecondMethod() {
- Console.WriteLine("SecondMethod");
- }
Fångat fel med meddelande
- static void Main(string[] args) {
- try {
- FirstMethod();
- }
- catch(ArgumentException e) {
- Console.WriteLine(e.Message);
- }
- SecondMethod();
- }
- static void FirstMethod() {
- Console.WriteLine("FirstMethod before throw");
- throw new ArgumentException("FirstMethod error");
- Console.WriteLine("FirstMethod after throw");
- }
Vad händer här?
- int? number = null;
- while (number == null) {
- Console.Write("Please enter a number: ");
- number = int.Parse(Console.ReadLine());
- }
- Console.WriteLine("You entered: " + number);
Hantera felinmatning
- int? number = null;
- while (number == null) {
- try {
- Console.Write("Please enter a number: ");
- number = int.Parse(Console.ReadLine());
- }
- catch(FormatException) {
- Console.WriteLine("You did not enter a number");
- }
- }
- Console.WriteLine("You entered: " + number);
Hantera alla tänkbara fel
- int? number = null;
- while (number == null) {
- try {
- Console.Write("Please enter a number: ");
- number = int.Parse(Console.ReadLine());
- }
- catch {
- Console.WriteLine("You did not enter a number");
- }
- }
- Console.WriteLine("You entered: " + number);
Fel flera nivåer in
- static void Main(string[] args) {
- try {
- FirstMethod();
- }
- catch(ArgumentException) {
- Console.WriteLine("Error caught");
- }
- Console.WriteLine("After catch");
- }
- static void FirstMethod() {
- Console.WriteLine("FirstMethod before SecondMethod");
- SecondMethod();
- Console.WriteLine("FirstMethod after SecondMethod");
- }
- static void SecondMethod() {
- Console.WriteLine("SecondMethod before error");
- throw new ArgumentException("SecondMethod error");
- Console.WriteLine("SecondMethod after error");
- }
Tvetydiga fel
- try {
- DisplayMessage(font: "Arial", message: "Hello!");
- }
- catch(ArgumentException) {
- // Which argument was wrong?
- }
Vad vi skulle vilja ha
- try {
- DisplayMessage(font: "Arial", message: "Hello!");
- }
- catch(MissingFontException) {
- ...
- }
- catch(TooLongMessageException) {
- ...
- }
Egna felklasser
- class MissingFontException : Exception {
- ...
- }
- class TooLongMessageException : Exception {
- ...
- }
- static void DisplayMessage(string font, string message) {
- ...
- throw new MissingFontException();
- ...
- throw new TooLongMessageException();
- ...
- }
Filhantering
Spara data till och läs data från hårddisken
Exempel.txt
- This is some text
- This is some text on a different line
- Some numbers: 1 2 3 4 5 6 7 8 9 0
Exempel.txt med synliga nyrader
- This is some text
- This is some text on a different line
- Some numbers: 1 2 3 4 5 6 7 8 9 0
Vad är datatypen på en fil?
Läsa en fil
- using System.IO;
- ...
- static void Main(string[] args) {
- string path = @"C:\Windows\System32\drivers\etc";
- string text = File.ReadAllText(path);
- string lines[] = File.ReadAllLines(path);
- }
Läsa en fil via relativ sökväg
- using System.IO;
- ...
- static void Main(string[] args) {
- string path = "Test.txt";
- string text = File.ReadAllText(path);
- }
"Be om ursäkt"
- using System.IO;
- ...
- static void Main(string[] args) {
- string path = "Test.txt";
- try {
- string text = File.ReadAllText(path);
- }
- catch (FileNotFoundException) {
- Console.WriteLine("Please create Test.txt");
- }
- }
"Be om tillåtelse"
- using System.IO;
- ...
- static void Main(string[] args) {
- string path = "Test.txt";
- if (File.Exists(path))
- string text = File.ReadAllText(path);
- }
- else {
- Console.WriteLine("Please create Test.txt");
- }
- }
Läsa och skriva
- using System.IO;
- ...
- static void Main(string[] args) {
- string path = "Test.txt";
- File.WriteAllText(path, "These are the contents");
- string text = File.ReadAllText(path);
- Console.WriteLine(text);
- }
Exempel på användningsområde
- using System.IO;
- ...
- static void Main(string[] args) {
- Console.Write("Enter discount code: ");
- string code = Console.ReadLine();
- string[] validCodes = File.ReadAllLines("Codes.txt");
- if (validCodes.Contains(code)) {
- Console.WriteLine("You will receive a discount!");
- }
- else {
- Console.WriteLine(code + " is not valid.");
- }
- }
Mer avancerat exempel
- using System.IO;
- ...
- static void Main(string[] args) {
- string[] lines = File.ReadAllLines("People.txt");
- List<Person> people = new List<Person>();
- foreach (string line in lines) {
- string[] parts = line.Split(',');
- string name = parts[0];
- int age = int.Parse(parts[1]);
- people.Add(new Person {
- Name = name,
- Age = age
- });
- }
- }