Types of Errors
✅ What Are Errors?
- Errors are problems or bugs in a program that cause it to behave unexpectedly or crash.
- Identifying and fixing errors is called debugging.
- Errors can occur at different stages: writing code, compiling, or running the program.
🧩 Types of Errors in Programming
- Syntax Errors
- Runtime Errors
- Logical Errors
- Compilation Errors
1️⃣ Syntax Errors
- Also called compile-time errors.
- Occur when the program code violates the grammar rules of the programming language.
- Detected by the compiler before the program runs.
- Prevent the program from compiling successfully.
Examples:
- Missing a semicolon or parenthesis.
- Misspelled keywords or variable names.
- Incorrect use of operators or statements.
Effect:
- Program does not compile until syntax errors are fixed.
2️⃣ Runtime Errors
- Occur while the program is running (execution phase).
- Caused by unexpected conditions or invalid operations.
- Detected only during execution.
- Often cause the program to crash or terminate abnormally.
Common Causes:
- Division by zero.
- Null reference or null pointer access.
- File not found when opening a file.
- Out of memory.
Example:
Dim x As Integer = 10
Dim y As Integer = 0
Dim z As Integer = x / y ' Causes runtime error (division by zero)
3️⃣ Logical Errors
- Occur when the program runs without crashing, but produces incorrect results.
- Caused by mistakes in algorithm or logic.
- Harder to detect because no error message is shown.
- Requires careful testing and debugging.
Examples:
- Using wrong formula for calculation.
- Incorrectly nested loops or conditions.
- Off-by-one errors in loops.
Effect:
- Program output is wrong or unexpected.
4️⃣ Compilation Errors
- Errors detected by the compiler during the compilation process.
- Includes syntax errors but also some semantic errors (type mismatches, undeclared variables).
- Prevent the program from generating an executable.
Additional Types of Errors
| Type | Description |
|---|---|
| Semantic Errors | Errors where the syntax is correct but the meaning is wrong. For example, using the wrong variable or wrong data type in expressions. |
| Linker Errors | Occur when external references (libraries) are missing or unresolved. |
| Logical/Design Errors | Errors due to flawed program design or algorithm. |
✅ Summary Table
| Error Type | When Detected | Cause | Effect |
|---|---|---|---|
| Syntax Error | Compile-time | Violation of language grammar | Program won’t compile |
| Runtime Error | Execution-time | Illegal operations during execution | Program crashes |
| Logical Error | Runtime/testing | Incorrect logic or algorithm | Incorrect program output |
| Compilation Error | Compile-time | Syntax and semantic errors | Program won’t compile |
📝 How to Handle Errors?
- Use debugging tools (breakpoints, step execution).
- Implement exception handling (
Try...Catchblocks). - Validate inputs to avoid runtime errors.
- Thoroughly test programs to find logical errors.
Structured Error Handling
✅ What is Structured Error Handling?
- Structured error handling is a programming technique used to detect, handle, and respond to runtime errors (exceptions) in a controlled way.
- It helps make applications robust and fault-tolerant by preventing unexpected crashes.
- In VB.NET, this is primarily done using Try...Catch...Finally blocks.
🧩 Components of Structured Error Handling
- Try Block
- Catch Block(s)
- Finally Block
1️⃣ Try Block
- Contains the code that might cause an error (exception).
- The program attempts to execute this code.
- If no error occurs, the program skips the Catch blocks and moves forward.
Example:
Try
' Code that may cause an exception
Dim x As Integer = 10 / 0 ' This will cause an exception
Catch ex As Exception
' Exception is handled here
End Try
2️⃣ Catch Block
- Defines how to handle exceptions when they occur in the Try block.
- You can have multiple Catch blocks to handle different types of exceptions.
- The exception object (commonly named
ex) provides information about the error.
Example:
Try
' Risky code here
Catch ex As DivideByZeroException
Console.WriteLine("Cannot divide by zero.")
Catch ex As Exception
Console.WriteLine("An error occurred: " & ex.Message)
End Try
3️⃣ Finally Block
- Optional block.
- Contains code that always executes, whether an exception occurs or not.
- Typically used to release resources like closing files, database connections.
Example:
Try
' Code that may throw exception
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
Finally
Console.WriteLine("Execution completed.")
End Try
✅ Flow of Structured Error Handling
| Step | Description |
|---|---|
| 1. Try | Execute code that may throw an exception |
| 2. Exception | If exception occurs, control moves to matching Catch block |
| 3. Catch | Handle the exception |
| 4. Finally | Execute cleanup code regardless of exception occurrence |
| 5. Continue | Continue program execution after error handling |
🧑💻 Complete Example
Try
Dim num1 As Integer = 10
Dim num2 As Integer = 0
Dim result As Integer = num1 / num2
Console.WriteLine("Result: " & result)
Catch ex As DivideByZeroException
Console.WriteLine("Error: Cannot divide by zero.")
Catch ex As Exception
Console.WriteLine("General error: " & ex.Message)
Finally
Console.WriteLine("Cleanup code runs here.")
End Try
✅ Advantages of Structured Error Handling
- Improves program reliability by managing exceptions gracefully.
- Separates error-handling code from regular code for clarity.
- Allows specific handling for different exception types.
- Ensures important cleanup code always runs.
- Makes debugging easier by capturing exception details.
⚠ Important Points
- Always catch specific exceptions before the general
Exceptionto avoid masking errors. - Avoid empty Catch blocks; always log or handle errors meaningfully.
- Use Finally block for resource cleanup.
- You can throw exceptions using
Throwkeyword to propagate errors.
📝 Summary Table
| Keyword | Purpose |
|---|---|
Try |
Code that may cause exceptions |
Catch |
Handles specific or general exceptions |
Finally |
Code that runs regardless of exceptions |