Introduction
Exception handling in C# is a mechanism used to handle runtime errors so that the normal flow of the program can be maintained.
Errors that occur during program execution are called exceptions. C# provides several keywords such as
try, catch, finally, throw, and throws to manage exceptions effectively.
Proper exception handling improves the reliability and stability of applications.
1. What is an Exception in C#?
An exception is an error that occurs during the execution of a program.
It interrupts the normal flow of the program and must be handled properly to avoid application crashes.
2. What is Exception Handling?
Exception handling is a mechanism used to detect and handle runtime errors so that the application can continue running smoothly.
3. Why is Exception Handling important?
- Prevents application crashes
- Maintains program flow
- Improves reliability
- Helps in debugging errors
4. What are the keywords used in Exception Handling?
The main keywords are:
- try
- catch
- finally
- throw
5. What is a try block?
The try block contains code that might generate an exception.
If an exception occurs, control is transferred to the catch block.
6. Syntax of try-catch block
try
{
// risky code
}
catch(Exception e)
{
// exception handling code
}
7. What is a catch block?
The catch block is used to handle the exception thrown in the try block.
8. What is a finally block?
The finally block contains code that always executes whether an exception occurs or not.
It is commonly used to release resources like files or database connections.
9. Example of try-catch-finally
try
{
int a = 10;
int b = 0;
int c = a / b;
}
catch(DivideByZeroException e)
{
Console.WriteLine("Cannot divide by zero");
}
finally
{
Console.WriteLine("Execution completed");
}
10. What is the base class for all exceptions?
The base class for all exceptions in C# is System.Exception.
11. What is a runtime error?
A runtime error occurs while the program is executing and causes the program to stop unexpectedly.
12. What is the difference between compile-time error and runtime error?
| Compile-time Error | Runtime Error |
|---|---|
| Occurs during compilation | Occurs during program execution |
| Detected by compiler | Detected while running program |
13. What is the throw keyword?
The throw keyword is used to manually throw an exception.
14. Example of throw keyword
throw new Exception("Custom error occurred");
15. What is a custom exception?
A custom exception is a user-defined exception class created by inheriting the Exception class.
16. Example of custom exception
class MyException : Exception
{
public MyException(string message) : base(message)
{
}
}
17. What is Stack Trace?
Stack trace provides information about the sequence of method calls that led to the exception.
18. What is Exception Message?
The message property describes the error that occurred.
19. Can multiple catch blocks be used?
Yes, multiple catch blocks can be used to handle different types of exceptions.
20. Example of multiple catch blocks
try
{
int a = int.Parse("abc");
}
catch(FormatException e)
{
Console.WriteLine("Invalid format");
}
catch(Exception e)
{
Console.WriteLine("General exception");
}
21. What is nested try block?
A try block inside another try block is called a nested try block.
22. What happens if an exception is not handled?
The program terminates and displays an error message.
23. What is the purpose of finally block?
It ensures that important cleanup code executes regardless of exceptions.
24. Can try exist without catch?
Yes, try can exist with finally without catch.
25. Can catch exist without try?
No, catch must always follow a try block.
26. What is FormatException?
Occurs when the format of input data is incorrect.
27. What is DivideByZeroException?
Occurs when a number is divided by zero.
28. What is NullReferenceException?
Occurs when accessing a null object reference.
29. What is IndexOutOfRangeException?
Occurs when accessing an array index that does not exist.
30. What is FileNotFoundException?
Occurs when a file being accessed does not exist.
31. What is InvalidOperationException?
Occurs when a method call is invalid for the object’s current state.
32. What is ArithmeticException?
Occurs when arithmetic errors happen in calculations.
33. What is OverflowException?
Occurs when a numeric calculation exceeds the allowed limit.
34. What is OutOfMemoryException?
Occurs when the system runs out of memory.
35. What is IOException?
Occurs during input/output operations like file reading.
36. What is finally used for in database connections?
To close the connection even if an error occurs.
37. What is rethrowing an exception?
Throwing the same exception again after catching it.
38. Example of rethrowing
catch(Exception e)
{
throw;
}
39. What is exception propagation?
When an exception moves from one method to another until it is handled.
40. What is global exception handling?
Handling exceptions at the application level.
41. What is best practice for exception handling?
- Use specific exception types
- Avoid empty catch blocks
- Log exceptions
- Release resources properly
42. What is the difference between throw and throw ex?
| throw | throw ex |
|---|---|
| Preserves original stack trace | Resets stack trace |
43. What is exception filtering?
A technique used to catch exceptions based on conditions.
44. Example of exception filter
catch(Exception e) when (e.Message.Contains("error"))
{
}
45. What is try-finally block?
Used when cleanup code must execute even without catch.
46. Can finally block contain return statement?
Yes, but it is not recommended.
47. What happens if exception occurs in finally?
It overrides the previous exception.
48. What is using statement?
It automatically disposes objects and helps manage resources.
49. Example of using statement
using(StreamReader sr = new StreamReader("file.txt"))
{
Console.WriteLine(sr.ReadToEnd());
}
50. What are advantages of exception handling?
- Improves program stability
- Handles runtime errors gracefully
- Helps debugging
- Maintains program flow
- Prevents sudden application termination
📢 Join Our WhatsApp Channel
💼 Get Daily IT Job Updates, Interview Preparation Tips & Instant Alerts directly on WhatsApp.
👉 Join WhatsApp Now📢 Join Our Telegram Channel
💼 Get Daily IT Job Updates, Interview Tips & Exclusive Alerts directly on Telegram!
👉 Join Telegram