C#
Error handling
Add input validation and error handling with try/catch.
Preventing invalid books
A user might accidentally enter a zero or negative page count. To prevent this, we will add a validation to the AddBook method: if pageCount <= 0, we throw a custom exception. This applies the exception mechanism you learned in the previous group to the project.
We will define a custom exception class called InvalidBookException; derive it from Exception and call the base(msg) constructor. Then we throw throw new InvalidBookException("...") inside AddBook and catch it with try/catch in Main.
Catching with try/catch
After throwing the custom exception, we must catch it with try/catch in Main. Inside the try block we call AddBook; if it throws, the catch (InvalidBookException ex) block runs and prints the error description via ex.Message.
This structure prevents the program from crashing on invalid input; instead, the user sees a meaningful error message. This pattern is very common in real applications: validation methods throw, and the calling code catches to give an appropriate response.
Exercise Add custom exception and try/catch
At the top of the file, define the InvalidBookException class; derive from Exception and add the constructor public InvalidBookException(string msg) : base(msg) { }. In AddBook, throw throw new InvalidBookException("Page count must be greater than zero"); when if (pageCount <= 0). In Main, add try { AddBook("Invalid Book", 0); } and catch (InvalidBookException ex) { Console.WriteLine("Error: " + ex.Message); }, then call ListBooks();.
Show example solution
Exception { public InvalidBookException(string msg) : base(msg) { } }. In AddBook, add if (pageCount <= 0) throw new InvalidBookException("Page count must be greater than zero");. In Main, add try { AddBook("Invalid Book", 0); } catch (InvalidBookException ex) { Console.WriteLine("Error: " + ex.Message); } ListBooks();
Example solution:
using System;
using System.Collections.Generic;
class Book
{
public string Title;
public int PageCount;
}
class InvalidBookException : Exception
{
public InvalidBookException(string msg) : base(msg) { }
}
class Program
{
static List<Book> books = new List<Book>();
static void PrintMenu()
{
Console.WriteLine("1 - Add");
Console.WriteLine("2 - List");
Console.WriteLine("3 - Exit");
}
static void AddBook(string title, int pageCount)
{
if (pageCount <= 0)
{
throw new InvalidBookException("Page count must be greater than zero");
}
books.Add(new Book { Title = title, PageCount = pageCount });
}
static void ListBooks()
{
foreach (Book b in books)
{
Console.WriteLine(b.Title + " - " + b.PageCount + " pages");
}
}
static void Main()
{
try
{
AddBook("Invalid Book", 0);
}
catch (InvalidBookException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
ListBooks();
}
}
Test yourself
-
Custom exception class
class InvalidBookException : ____
{
public InvalidBookException(string msg) : base(msg) { }
}
Show answer
class InvalidBookException : Exception
{
public InvalidBookException(string msg) : base(msg) { }
}
-
When should AddBook throw an exception?
static void AddBook(string title, int pageCount)
- Always
- Only when title is empty
- Never
- When pageCount <= 0
Show answer
When pageCount <= 0 — Correct! Throw InvalidBookException when pageCount <= 0.
-
Match each element with its description
- InvalidBookException
- Custom exception
- : base(msg)
- Base constructor
- catch (InvalidBookException ex)
- Catch block
Next lesson: Putting it together