C# - Compile time vs run time exception
In C#, exceptions can occur at compile time or runtime, the key differences between them are as follows:
-
Occurrence:
-
Compile-time exceptions occur during the compilation phase of the code. These errors are detected by the compiler when it translates the source code into an intermediate language (IL) or executable code. The program cannot be executed until all compile-time errors are resolved.
- Runtime exceptions occur during the execution phase of the code when the program is running. These errors are not detected by the compiler and arise while the program is executing due to various reasons such as invalid input, logical issues, or unexpected data.
-
Detection:
-
Compile-time exceptions are detected and reported by the compiler, which stops the code from being successfully compiled into an executable program. The developer must resolve these errors before running the program.
- Runtime exceptions are detected during program execution. The program will run until the exception is encountered, and at that point, the runtime environment will handle the exception based on how it is caught and handled in the code.
-
Handling:
-
Compile-time exceptions must be fixed by the developer in the source code before the program can be successfully compiled and run.
- Runtime exceptions are typically handled using structured exception handling mechanisms, such as try-catch-finally blocks. The program can attempt to recover from the exception, log the error for analysis, or gracefully terminate, depending on the specific handling logic implemented by the developer.
Compile-time exception example (Syntax error):
class CompileTimeErrorExample
{
static void Main(string[] args)
{
int number = "123"; // This line will cause a compile-time exception
// because you cannot assign a string to an integer variable.
}
}
This code will not compile because you're trying to assign a string literal to an integer variable. The compiler will throw an error similar to:
error CS0029: Cannot implicitly convert type 'string' to 'int'
Runtime exception example::
using System;
class RuntimeErrorExample
{
static void Main(string[] args)
{
int[] numbers = new int[5];
try
{
int numberAtIndex10 = numbers[10]; // This line will cause a runtime exception
// because there is no index 10 in the array.
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("An exception occurred: " + ex.Message);
}
}
}
When you run this program, the output will be something like:
An exception occurred: Index was outside the bounds of the array.
In the runtime exception example, the code compiles successfully because there's nothing syntactically wrong with it. However, when the program runs, it tries to access an array element at an index that does not exist, which causes a IndexOutOfRangeException
to be thrown. The try-catch
block is used to catch the exception and handle it by displaying a message to the user.
In summary, exceptions at compile-time are identified by the compiler while converting the source code into an executable form and must be resolved prior to the execution of the program. Runtime exceptions occur during program execution and require proper handling using try-catch blocks to ensure that the program handles errors gracefully and maintains robustness.