Chapter 4 and 5
Chapter 4 and 5
Chapter 4 and 5
Exception Handling
C# Exceptions
When executing C# code, different errors can occur: coding errors made by the
programmer, errors due to wrong input, or other unforeseeable things.
When an error occurs, C# will normally stop and generate an error message.
The technical term for this is: C# will throw an exception (throw an error).
Syntax
try
catch (Exception e)
}
Consider the following example, where we create an array of three integers:
Console.WriteLine(myNumbers[10]); // error!
If an error occurs, we can use try...catch to catch the error and execute some
code to handle it.
In the following example, we use the variable inside the catch block ( e) together
with the built-in Message property, which outputs a message that describes the
exception:
Example
try
Console.WriteLine(myNumbers[10]);
catch (Exception e)
Console.WriteLine(e.Message);
Console.WriteLine(myNumbers[10]);
catch (Exception e)
Finally
The finally statement lets you execute code, after try...catch, regardless of the
result:
Example
try
Console.WriteLine(myNumbers[10]);
}
catch (Exception e)
finally
The throw statement is used together with an exception class. There are many
exception classes available in
C#: ArithmeticException, FileNotFoundException, IndexOutOfRangeException, TimeOutExce
ption, etc:
Example
static void checkAge(int age)
else
checkAge(15);
Example
checkAge(20);
Manipulating Files
C# Files
C# provides the File class which is used to perform various operations like
creating a file, opening a file, reading and writing a file etc.
The above image shows some of the classes under the System.IO namespace.
Among these classes, we will learn about the File class and it's methods to
work with files in C#.
Create a new file, writes the specified string to the file, and then
WriteAllText()
closes the file
ReadAllText() Opens a text file, reads all lines of the file, and then closes the file.
Opens a file, appends the specified string to the file, and then
AppendAllText() closes the file. If the file does not exist, this method creates a file,
writes the specified string to the file, then closes the file.
Create a File in C#
We use the Create() method of the File class to create a new file in C#. For
example,
Note: If the file already exists, the Create() method overwrites the file.
using System;
using System.IO;
class Program
{
static void Main()
{
// path of the file that we want to create
string pathName = @"C:\Program\myFile.txt";
Output
File is created.
using System;
using System.IO;
class Program
{
static void Main()
{
string pathName = @"C:\Program\myFile.txt";
}
}
Here, the Open() method opens myFile.txt file. Here, FileMode.Open specifies -
open the existing file.
Note: A file stream is a sequence of bytes used to hold file data. Every file
contains at least one file stream.
Write to a File
We use the WriteAllText() method of the File class to write to a file. The
method creates a new file and writes content to that file.
Let's see an example to write to a file in C#.
using System;
using System.IO;
class Program
{
static void Main()
{
string pathName = @"C:\Program\myFile.txt";
}
}
The above image shows the myFile.txt file that contains the text "Hello World" .
Note: If the file already exists, the WriteAllText() method overwrites the file.
Read a File in C#
We use the ReadAllText() method of the File class to read contents of the file.
The method returns a string containing all the text in the specified file.
Let's read the content of the file myFile.txt where we had written "Hello World" .
using System;
using System.IO;
class Program
{
static void Main()
{
string pathName = @"C:\Program\myFile.txt";
Console.WriteLine(readText);
}
}
Output
Hello World
The ReadAllText() method reads the file myFile.txt and returns "Hello World" .