Exception, File
Exception, File
NET
Module 2
String Handling
Example
public static void Main(string[] args)
{ string s1 = "Hello ";
string s2 = (String)s1.Clone();
Console.WriteLine(s1);
Console.WriteLine(s2);
}
Quoted string literals
● try − A try block identifies a block of code for which particular exceptions is
activated. It is followed by one or more catch blocks.
● catch − A program catches an exception with an exception handler at the
place in a program where you want to handle the problem. The catch
keyword indicates the catching of an exception.
● finally − The finally block is used to execute a given set of statements,
whether an exception is thrown or not thrown. For example, if you open a
file, it must be closed whether an exception is raised or not.
● throw − A program throws an exception when a problem shows up. This is
Exception Classes
● ThreadLocal
● ThreadPool
System.Threading.Thread class
The following are the most common instance members of the
System.Threading.Thread class:
● Name
A property of string type used to get/set the friendly name of the thread instance.
● Priority
A property of type System.Threading.ThreadPriority to schedule the priority of
threads.
● IsAlive
A Boolean property indicating whether the thread is alive or terminated.
● ThreadState
A property of type System.Threading.ThreadState, used to get the value
containing the state of the thread.
System.Threading.Thread class
● start()
Starts the execution of the thread.
● abort()
Allows the current thread to stop the execution of the thread permanently.
● suspend()
Pauses the execution of the thread temporarily.
● resume()
Resumes the execution of a suspended thread.
● join()
Make the current thread wait for another thread to finish.
● sleep()
Temporarily suspend the current execution of the thread for specified milliseconds.
Thread Synchronization
● Synchronization is a technique that allows only one thread to access the resource
for the particular time. No other thread can interrupt until the assigned thread
finishes its task.
● In multithreading program, threads are allowed to access any resource for the
required execution time. Threads share resources and executes asynchronously.
● Accessing shared resources (data) is critical task that sometimes may halt the
system. We deal with it by making threads synchronized.
● It is mainly used in case of transactions like deposit, withdraw etc.
● No Thread Interference
Lock
● We can use C# lock keyword to execute program
synchronously.
● It is used to get lock for the current thread, execute the task and
then release the lock.
● It ensures that other thread does not interrupt the execution until
the execution finish.
FILE I/O
● A 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.
● The stream is basically the sequence of bytes passing through the communication
path.
There are two main streams:
● Input stream
● Output stream
● The input stream is used for reading data from file (read operation) and the output
stream is used for writing into the file (write operation).
I/O Classes
➔ The System.IO namespace has various classes that are used for performing
numerous operations with files, such as creating and deleting files, reading from
or writing to a file, closing a file etc.
➔ The following are the non-abstract classes in the System.IO namespace −
● BinaryReader : Reads primitive data from a binary stream.
● BinaryWriter : Writes primitive data in binary format.
● BufferedStream :A temporary storage for a stream of bytes.
● Directory :Helps in manipulating a directory structure.
● DirectoryInfo :Used for performing operations on directories.
● DriveInfo :Provides information for the drives.
● File :Helps in manipulating files.
● StringReader :Is used for reading from a string buffer.
● StringWriter :Is used for writing into a string buffer.