Loading...

C# Throwing

Write the stop yourself with throw. Keep the work in Pine with Main. Throw ArgumentException with the message empty before any print of water. Run with dotnet run. The program stops on that throw line; water does not print. A second demo uses cups at 0 so an if enters and throws. A third demo uses cups at 3 so the if is skipped and the screen prints water. Leave reading files for the next page. Keep the starting public class named Pine.

You write the stop

Earlier pages showed a stop that appeared because a line broke a rule — a bad array index threw IndexOutOfRangeException. This page is the other direction. You choose the exception type and you write throw so the stop happens on purpose. Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete leftover try and catch demos that print blocked then after. Delete leftover bare slots[5] stops. Delete any ReadLine line. The program must not wait for typing.
At the top keep using System;. Inside Main, throw before any quiet success print:
throw new ArgumentException("empty");
Do not place a Console.WriteLine("water"); on the next line after that bare throw in the same path — C# treats that as unreachable and the project may fail to build. The stop itself is the lesson. The words in quotes are the message. The type name is ArgumentException. You are not dividing by zero and you are not reading slots[5]. The stop is there because you wrote throw.

Pine bare throw stops before water

csharp
using System;

public class Pine
{
    public static void Main()
    {
        throw new ArgumentException("empty");
    }
}

Save, then run with dotnet run. The run stops on the throw line. The terminal names ArgumentException and shows the message empty. A print of water after that throw does not run, because the program already stopped. Prefer the bare throw file without a dead print after it so the project builds cleanly — the stop itself is the lesson.
If the terminal waits for keyboard input, a ReadLine call from an older exercise is still in a file. Delete that call. If you see blocked then after, you are still running a try-catch file — switch to the throw teaching file with dotnet run. If you see IndexOutOfRangeException from slots[5], an exceptions leftover is still active; replace it with the throw line for this lesson. If the screen shows True then 4, a nullable leftover is still the path you are running; replace it with the throw demo.
Notice that the project may still build before you run when braces and semicolons match. The stop is a run-time stop you authored, not a missing semicolon. Spelling of the exception type matters: ArgumentException. Saying only "error" is too vague for this lesson — keep the full type name when you describe the stop you wrote.

Zero cups enters the if and throws

A second program starts fresh. Store cups as 0. If cups is less than 1, throw the same ArgumentException with message empty. Keep a print of water after the if.

Pine zero cups throws

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int cups = 0;
        if (cups < 1)
        {
            throw new ArgumentException("empty");
        }
        Console.WriteLine("water");
    }
}

Run with dotnet run. Zero is less than one, so the if is entered and throw runs. The screen shows the same stop: ArgumentException and empty. The word water is not printed. Nothing on this page catches the stop, so the program ends there. That is intentional — last page practiced catch; this page practices writing the throw.
Walk the braces once. The throw line sits inside the if block. The water print sits outside the if. Once throw runs, the program has already stopped, so water is never reached. The indent and braces are what put the throw inside the check. Keep using System; for console writes on the cups demos. Do not nest a second Main.

Three cups skips throw and prints water

A third program starts fresh too. Set cups to 3. Keep the same if and the same throw and the same water print.

Pine three cups prints water

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int cups = 3;
        if (cups < 1)
        {
            throw new ArgumentException("empty");
        }
        Console.WriteLine("water");
    }
}

Run again with dotnet run. Three is not less than one, so the if is not entered. The throw never runs. The screen prints water. There is no stop. That contrast is the checkpoint: the throw only fires when your condition says the value is not usable.
Do not invent a catch around these teaching files on this page. You already practiced catch on the try-catch page. Here you need to see the stop you wrote, and to see the path that skips the throw. Also do not replace ArgumentException with IndexOutOfRangeException for empty cups — that name belonged to a bad array index, not to a value check you authored with throw. Keep leftover try-catch demos out of the throw files so the screen cannot show blocked then after by mistake when you meant a throw stop or water.

The name you wrote

The type you threw is ArgumentException, not IndexOutOfRangeException and not a bare word empty as the type. The message empty is what you chose to show next to the name. If you catch this stop later, the catch clause must name ArgumentException. A catch for NullReferenceException would not handle it. That catch practice is not required on these Pine files today.
ArgumentException is a common beginner run-time exception in C#, so these small Main demos do not need extra ceremony. Stay with this one clear type and message. You do not need a catalog of every throwable class before the next page opens a file. Keep the array crash story as history — this page is about a stop you wrote with throw.

Compared with catch on try-catch

On the try-catch page, dotnet run with a catch printed blocked then continued to after. On this page, dotnet run causes a stop on purpose and does not catch it. The array story is history. Keep try-catch files only if they still sit nearby as older demos; run the throw teaching file for the demos so the screen matches this lesson. If dotnet run waits for typing, ReadLine is still in the file — delete it.

You can throw ArgumentException("empty") and see the run stop before water. With cups at 0, the if enters and the same stop happens. With cups at 3, throw never runs and the screen prints water. Run each kept demo with dotnet run in turn. The name of this stop is ArgumentException. Think of throw as writing the stop yourself. Pine stays the door you open with dotnet run. The next page is reading a file.
Keep the bare throw for a clear stop you wrote. Keep the cups pair for a condition that can skip the throw. Keep file reading off this page. Those three habits are the checkpoint before you move on. When water appears only on the cups-at-three run, throwing did its job for this lesson and you are ready for the next topic about files.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top