Does finally get executed if the code throws an error?
👉 Yes, the finally block always executes, even if an exception is thrown — unless the
program is forcefully terminated (e.g., Environment.Exit(), power failure).
This ensures that cleanup tasks are always performed.
9. What is the difference between System exceptions and Application
exceptions?
System exceptions:
o Built-in exceptions provided by the .NET framework.
o Derived from System.SystemException.
o Examples: NullReferenceException, IndexOutOfRangeException,
DivideByZeroException.
Application exceptions:
o User-defined or logical exceptions created by developers.
o Derived from System.ApplicationException.
o Examples: InvalidAgeException, NegativeSalaryException.
10. What is the difference between throw and throw ex ?
👉 Both are used to raise exceptions, but they behave differently:
throw → re-throws the original exception while preserving the original stack trace
(best practice).
throw ex → re-throws the exception but resets the stack trace, losing the original
error location (not recommended).
🔹 Example:
catch(Exception ex) {
throw; // preserves stack trace (preferred)
// throw ex; // resets stack trace (bad practice)
}
✅ Answer: Always use throw; instead of throw ex; when re-throwing.
1 — Short definition: file, stream,
input/output stream
File — a named collection of bytes persisted on disk (or other storage) and addressed
by a path (folder(s) + filename).
Stream — an abstraction representing a sequence of bytes flowing through a channel
(file, network, memory). A stream lets you read and/or write bytes sequentially or
randomly (depending on implementation).
Input stream — used to read bytes or characters.
Output stream — used to write bytes or characters.
Streams decouple programs from exact storage details and allow a uniform API for files,
memory buffers, sockets, etc.
2 — System.IO: the important classes (what they
do, when to use them)
File / FileInfo — convenience helpers and object wrapper for file operations (create,
copy, delete, move, existence checks). File is static and quick for short tasks.
FileInfo is an object with instance methods and cached info.
Directory / DirectoryInfo — like File/FileInfo but for directories (create, delete,
enumerate).
FileStream — low-level byte stream for reading/writing files. Derives from Stream.
Use when you need precise control (seek, position, read/write bytes, performance
tuning).
StreamReader / StreamWriter — high-level text readers/writers built on top of byte
streams; they handle decoding/encoding characters using encodings (UTF-8, UTF-16,
etc.) and buffering.
BinaryReader / BinaryWriter — read/write primitive types (ints, floats, strings) in
binary form.
MemoryStream — a stream backed by memory (byte array) — useful for building
data in memory, conversions, tests.
BufferedStream — adds a buffer around another stream to improve performance for
many small reads/writes.
Path — operations on path strings (Combine, GetDirectoryName, GetFileName,
GetExtension).
DriveInfo — info about disks (free space, drive type).
3 — FileStream constructor and the
parameters (what each means)
Common constructor:
new FileStream(path, FileMode mode, FileAccess access, FileShare share, int
bufferSize = 4096, FileOptions options = FileOptions.None)
Important parameters:
FileMode — how to open the file:
o Create — create a new file; if it exists, overwrite (truncate).
o CreateNew — create a new file; if it exists, throw an exception.
o Open — open an existing file; if it doesn’t exist, throw.
o OpenOrCreate — open if exists; otherwise create.
o Append — open if exists and position at end; or create; only for writing (you
cannot read from an Append stream).
o Truncate — open existing file and truncate to zero length (must have write
access).
FileAccess — permitted operations: Read, Write, or ReadWrite.
FileShare — how other processes/threads may open the file while you have it open:
o None — deny others any access.
o Read — allow others to open for reading.
o Write — allow others to open for writing.
o ReadWrite — allow both read and write.
o Delete — (Windows) allow deleting the file while opened.
o Inheritable — allow child processes to inherit handle (rare).
Choose the smallest share that still permits needed concurrency; defaulting to
None avoids unexpected conflicts.
bufferSize — size of internal buffer (larger can improve throughput for large
transfers).
FileOptions — hints to OS (SequentialScan, RandomAccess, Asynchronous) that
may affect caching and performance.