Exception Handling and Debugging
Exception Handling and Debugging
1
Introduction to exception handling
•What is debugging ?
• The process of trying to track down errors in your program.
• It can also refer to handling any potential errors that may occur.
•There are three types of errors are Design-time errors, Run-time errors
and Logical errors
3
A. Design-time errors
Design-Time errors are ones that you make before the program even runs.
In fact, for Design-Time errors, the program won’t run at all, most of the
time.
You’ll get a popup message telling you that there were build errors, and
asking would you like to continue.
Design-Time errors are easy enough to spot because the C# software will
underline them with a wavy colored line.
12/26/24 4
colored lines to show design
time errors
The blue wavy lines are known as Edit and Continue issues, meaning
that you can make change to your code without having to stop the
program altogether.
Red wavy lines are Syntax errors, such as a missing semicolon at the
end of a line, or a missing curly bracket in an IF Statement.
Green wavy lines are Compiler Warnings. You get these when C#
spots something that could potentially cause a problem, such as
declaring a variable that’s never used. 5
B. Run-Time Errors
Run-Time errors are ones that crash your program.
The program itself generally starts up OK.
It’s when you try to do something that the error surfaces.
A common Run-Time error is trying to divide by zero.
6
C. Logic Errors
Logic errors are ones where you don’t get the result you were
expecting. Errors in programming logic
The program generally won’t “bug out” on you.
12/26/24 7
• What is Exception handling? Its mechanism to handle errors happened in
programming lanagueg and is an important ingredient of a robust application and
should be included in the application design of every .NET program.
• Introduction
• How to open a Text File
• Read from a Text file
• Write to a Text File
• How to Copy, Move and Delete a File
11
Introduction
File is a collection of data stored in a disk with a specific name and a
directory path.
When a file is opened for reading or writing, it becomes a stream.
1.Input Stream: is used for reading data from file (read operation)
2.Output Stream: is used for writing into the file (write operation).
12
C# I/O Classes
System.IO namespace has various classes that are used for
performing numerous operations with files,
This file operations include;
13
Commonly used non-abstract classes in the System.IO
I/O Class Description
namespace
BinaryReader Reads primitive data from a binary stream.
BinaryWriter Writes primitive data in binary format.
15
General file Manipulating
methods
Checking if a file exists
if(File.Exists("MyFile.txt"))
{
Console.WriteLine("File exist");
}
else
{
Console.WriteLine("File does not exist");
}
16
Cont’d…
Creating a new file
File.Create(FILE_DIR + "MyFile.bin");
Copying a file
File.Copy("MyFile.txt","MyFile2.txt",true);
Note the third argument in the above Copy method is used to indicate
whether the [second] file should be overwritten or not if it already
exists.
17
Cont’d…
Delete a file
File.Delete("MyFile2.txt");
Moving a file
File.Move("MyFile.txt","MySubDir\\MyMovedFile.txt");
18
Text files
Writing text to a text file
StreamWriter myFile = File.CreateText("MyFile.txt");
myFile.WriteLine("This text will be stored in a text file");
myFile.Close();
Appending text to a text file
StreamWriter myFile = File.AppendText("MyFile.txt");
myFile.WriteLine("This text will be appended to the file");
myFile.Close();
19
Text files
Reading from a text file
string s;
StreamReader myFile = File.OpenText("MyFile.txt");
while ((s = myFile.ReadLine()) != null)
{
Console.WriteLine(s);
}
myFile.Close();
Event Driven Programming Chapter-5:
12/26/24 20
File Manipulation
Classes File and Directory
21
Classes File and
Directory
static Method Description
12/26/24
Static methods of Directory 24
Event Driven Programming Chapter-5:
12/26/24 25
File Manipulation
using System;
using System.Windows.Forms;
using System.IO;
27
Thank
you!
28