0. The Real Goal of This Lesson
The goal of this lesson is to physically understand one sentence:
“Why code that compiles successfully
can still crash while running.”
If this is not clear, then:
-
TryParsebecomes memorization - repeated input handling makes no sense
- writing stable programs becomes impossible
Everything after this point turns into empty syntax.
1. Why Everything Worked Until Now
Up to this point, we have been writing code like this:
var input = Console.ReadLine();
int number = int.Parse(input);
And the user entered:
5
Result:
- No error
- Program runs normally
This makes it very easy to assume:
“
int.Parseworks fine.”
This assumption is dangerous.
2. What If the User Enters This?
ABC
At this moment, something fundamentally different happens.
3. What Actually Happens at Runtime
When the program runs and the user types ABC:
- Execution suddenly stops
- It looks like a breakpoint was hit
- A red error window appears
This is not a bug.
This is expected behavior.
4. What That Red Window Really Is
The message usually looks like this:
System.FormatException
Input string was not in a correct format
This is an exception.
5. What an Exception Really Is
An exception is a signal raised at runtime
indicating that the program cannot continue normally.
Key points:
- Not a compile error
- Not a syntax error
- Happens during execution
6. Compile-Time Errors vs Runtime Errors
Compile-Time Error
int x = "ABC"; // compile-time error
- The program never runs
- The IDE blocks execution
- Must be fixed before running
This is safe.
Runtime Error
int.Parse("ABC"); // runtime exception
- Compilation succeeds
- Program starts running
- Program crashes during execution
This is dangerous.
7. Why Doesn’t C# Just Convert It to 0?
A very common question:
“Why not just treat invalid input as 0?”
C#’s answer is deliberate:
“That decision belongs to the developer.”
Because what does this mean?
-
"ABC"→ 0 ? -
" "→ 0 ? -
"999999999999"→ 0 ?
These all mean different things.
Automatically choosing a value would silently corrupt intent.
So C# says:
“I don’t know what you want.
This is an exceptional situation.”
8. The Exact Reason the Program Crashed
The real execution flow is:
-
int.Parse(input)is executed - The string is not a valid number
- A
FormatExceptionis thrown - No one handles it
- The program terminates immediately
This is called an unhandled exception.
9. Exceptions Are Not Failures
This perspective is critical.
- Exception = bug ❌
- Exception = expected risk ⭕
Common exception scenarios:
- Division by zero
- Accessing an empty collection
- Missing files
- Parsing non-numeric input
These are normal real-world situations.
10. The Mental Shift You Must Make
Beginner mindset:
- “Why did my program crash?”
- “Something is wrong with my code.”
Developer mindset:
“This is an exceptional case.”
“This is where I must decide what happens next.”
11. Exception Messages Are Hints
Input string was not in a correct format
This message already tells you:
- The input format is wrong
- The value is not numeric
- Parsing failed
If you can read the message, the solution direction is obvious.
12. Searching Errors Is a Core Developer Skill
Copying this message into a search engine leads to:
- Thousands of identical cases
- Explanations
-
TryParsesolutions
Knowing what to search is part of being a developer.
13. Why the Next Lesson Is Inevitable
Current situation:
- We want numeric input
- User input is unreliable
- Unhandled exceptions crash the program
This leads to one unavoidable question:
“How do we handle this without crashing?”
That question naturally leads to:
TryParse- exception handling
- defensive programming
One-Line Summary
An exception does not mean your program is wrong —
it means C# is asking you to take responsibility for this situation.
Top comments (0)