Database Reference
In-Depth Information
Property Name
Description
StackTrace
Defines a string representation of the call stack when the exception was thrown
TargetSite
Represents the method that throws the current exception
What Causes an Exception to Occur
Before we get into more details of how to handle an exception, let's see what an exception looks like
when it occurs and how an application behaves in such a situation.
Many organizations today depend on log files to trace the activities happening on a system; you
might have even seen or read some setup.log files. Hence, file handling is an important concept that can
apply in many situations. For example, whatever you enter into a text box can be recorded into a log file,
and later this information can be read from the stored file on the disk.
Assume you are given an application that reads the file path and file name from the user and opens
the file. Normally, the application works fine, but in this case, an incorrect file name or path is provided,
so an exception occurs.
Try It: Creating a File-Handling Application
In this exercise, you'll create a Windows Forms application with four labels, four text boxes, and two
buttons. The application will accept some text and then save/write it to a file on disk; it will also take a
file path as input and read the file content for you.
1. Create a new Windows Forms application project named Chapter11. When
Solution Explorer opens, save the solution.
2. Rename the Chapter11 project to FileHandling and then rename Form1 to
FileExceptionHandling.
3. Change the Text property of the FileExceptionHandling form to File-
Read/Write.
4. Drag a Label control onto the form, and position it toward the top-left corner.
Select this Label control, navigate to the Properties window, and set the
following properties:
a. Set the Name property to lblPathWrite.
b. Set the Text property to Enter File Write Path.
5. Drag a TextBox control next to the Label control named lblPathWrite that you
just dragged onto the form. Select this TextBox control, navigate to the
Properties window, and set the following properties:
a. Set the Name property to txtFileWritePath.
b. Set the Size property to 301, 20.
6. Drag a Button control next to the TextBox control you just dragged onto the
form. Select this Button control, navigate to the Properties window, and set the
following properties:
 
Search WWH ::




Custom Search