👨‍💻
Guide
Laura's GithubSimon's Github
  • What is this gitbook for?
  • 🛠️Useful tools
    • 🏁Header files
    • 🧱C Structures
    • 🔗Linked Lists (todo)
    • 📄Makefiles
    • 🔄Switch statement
    • 🗃️File descriptors (FD)
  • 🖌️MiniLibX
    • MiniLibX Helper Function
    • MiniLibX Hook Examples
  • 0️⃣ Rank 00
    • Libft
      • 📑LIBC functions
        • ft_isalpha
        • ft_isdigit
        • ft_isalnum
        • ft_isascii
        • ft_isprint
        • ft_strlen
        • ft_memset
        • ft_bzero
        • ft_memcpy
        • ft_memmove
        • ft_strlcpy
        • ft_strlcat
        • ft_toupper
        • ft_tolower
        • ft_strchr
        • ft_strrchr
        • ft_strncmp
        • ft_memchr
        • ft_memcmp
        • ft_strnstr
        • ft_atoi
        • ft_calloc
        • ft_strdup
      • 📑Additional functions
        • ft_substr
        • ft_strjoin
        • ft_strtrim
        • ft_split
        • ft_itoa
        • ft_strmapi
        • ft_striteri
        • ft_putchar_fd
        • ft_putstr_fd
        • ft_putendl_fd
        • ft_putnbr_fd
      • 📑Bonus functions
        • ft_lstnew
        • ft_lstadd_front
        • ft_lstsize
        • ft_lstlast
        • ft_lstadd_back
        • ft_lstdelone
        • ft_lstclear
        • ft_lstiter
        • ft_lstmap
  • 1️⃣ Rank 01
    • Born2beRoot
      • 📠What's a virtual machine ?
      • 📠Install your virtual machine
      • 📠P2P Evaluation - Questions
    • ft_printf
      • ▪️Variadic functions
      • ▪️Building the thing
    • get_next_line
      • ▪️open() & read()
      • ▪️Static variables
      • ▪️Building the thing
      • ▪️Commented solution
  • 2️⃣ Rank 02
    • so_long
      • ▪️Understand so_long
      • ▪️Core concepts
      • ▪️Building the thing
    • pipex
      • ▪️Understand pipex
      • ▪️Functions used
      • ▪️Building the thing
    • minitalk
      • ▪️Understand minitalk
      • ▪️Functions used
      • ▪️Building the thing
    • push_swap
      • ▪️Algorithms
      • ◾Building the thing
    • FdF
      • 🗡️Understand FdF
      • 🗡️Graphics programming
      • 🗡️Building the thing
  • 3️⃣ RANK 03
    • Philosophers
      • ▪️Understand Philosophers
      • ▪️Functions used
      • ▪️Building the thing
    • Minishell
      • ▪️Understand Minishell
      • ▪️Functions
      • ◾Building the thing
  • 4️⃣ RANK 04
    • CPP (00 - 04) (doing)
      • CPP00
      • CPP01
      • CPP02
      • CPP03
      • CPP04 (doing)
    • NetPractice
      • Theory
      • Level 1 & 2
    • MiniRT
      • Understand MiniRT
      • Building the thing
  • 5️⃣ RANK 05
    • CPP (05-09) (to-do)
      • CPP05
      • CPP06 (to-do)
      • CPP07
      • CPP08 (to-do)
      • CPP09 (to-do)
    • Inception (doing)
      • 🕓The basics (Docker, Images, etc...)
      • Project Files
    • webserv (to-do)
  • 6️⃣ rank 06
    • ft_transcendence (to-do)
  • 🛂Exams
    • Exam Rank 02
      • Level 1
        • first_word
        • fizz_buzz
        • *ft_putstr
        • *ft_strcpy
        • *ft_strlen
        • ft_swap
        • repeat_alpha
        • rev_print
        • rot_13
        • rotone
        • search_and_replace
        • ulstr
      • Level 2
        • alpha_mirror
        • camel_to_snake
        • do_op
        • *ft_atoi
        • *ft_strcmp
        • ft_strcspn
        • ft_strspn
        • *ft_strdup
        • ft_strpbrk
        • ft_strrev
        • inter
        • last_word
        • ft_is_power_2
        • max
        • print_bits
        • reverse_bits
        • wdmatch
        • swap_bits
        • union
        • snake_to_camel
      • Level 3
        • add_prime_sum
        • epur_str
        • expand_str
        • ft_atoi_base
        • ft_list_size
        • ft_range
        • ft_rrange
        • hidenp
        • lcm
        • paramsum
        • pgcd
        • print_hex
        • rstr_capitalizer
        • str_capitalizer
        • tab_mult
      • Level 4
        • flood_fil
        • fprime
        • ft_split
        • ft_itoa
        • ft_list_foreach
        • ft_list_remove
        • rev_wstr
        • rotstring
        • sort_int_tab
        • sort_list
    • Exam Rank 03
    • Exam Rank 04
    • Exam Rank 05
      • Module 0
  • 👥Team
Powered by GitBook
On this page

Was this helpful?

  1. 5️⃣ RANK 05
  2. CPP (05-09) (to-do)

CPP05

Main topics

Repetition and Exceptions (try, throw & catch

In this module, you'll learn how to create exceptions and then use them in your code.

First, let's talk FOOD (because i'm hungry): Imagine you're baking a cake.

  1. Mixing Bowl: This is where you combine ingredients, like mixing flour, eggs, and sugar. When you're blending things together, sometimes a small mistake can happen.

  2. Eggshell: If you accidentally drop an eggshell into your mix, you wouldn't want it in your cake, right? So, you remove the eggshell to keep the cake perfect. Similarly, in code, when something unexpected occurs, you "throw away" the issue by sending an error message.

  3. Safety Net: Now, picture having a safety net under your mixing bowl. If an eggshell falls in (an error occurs), the safety net catches it. Then you can decide what to do with the eggshell, like discarding it or acknowledging the mistake. In code, the safety net is like the "catch" block, helping you handle unexpected problems that occur during your program's execution.

The metaphor may be a bit lame, but we're going to do exactly the same thing (well... almost) in C++.

Let's take a very easy example and see step by step what it does to better understand these new concepts :)

Here we have :

#include <iostream>

int main() {
    int dividend, divisor, result;

    std::cout << "Enter the dividend: ";
    std::cin >> dividend;

    std::cout << "Enter the divisor: ";
    std::cin >> divisor;

    try 
    {
        if (divisor == 0) 
        {
            throw "Division by zero is not allowed!";  // Throw an exception
        }

        result = dividend / divisor;
        std::cout << "Result: " << result << std::endl;
    }
    catch (const char* errorMessage) 
    {
        std::cerr << "Error: " << errorMessage << std::endl;
    }

    return 0;
}

The code consists of three main parts. The "try" (miying bowl), "throw" (eggshell) & "catch"(safety net) blocks:

  1. try block (l. 12-19): This is where you put code that might generate an exception. In this case, it checks if the divisor is zero, which would result in division by zero.

  2. throw (l.16 && also 14-17): If a condition inside the try block is met (in this case, if divisor is zero), the program throws an exception. This means it raises an error message ("Division by zero is not allowed!").

  3. catch block: This block "catches" the exception if it's thrown. It's like a safety net that handles the error. In this code, it catches exceptions of type const char*, which is a pointer to a string (the error message).

So, in simple terms, if you try to divide by zero (which is not allowed in math), the code throws an exception with the error message. The catch block then catches this exception and displays the error message.

If you enter valid numbers, the division is performed, and no exception is thrown. In this case, the code displays the result.

This mechanism helps you gracefully handle errors and prevent your program from crashing when unexpected issues occur.

And... that's about it. All CPP05 exercises are about playing with exceptions.

You'll also have to create your own exceptions: class exceptions. It won't be too complicated, so I'll let you find the answer you to search on Internet. (Spoiler: the exception you create must inherit from the class... Exception ;-) )

PreviousCPP (05-09) (to-do)NextCPP06 (to-do)

Last updated 1 year ago

Was this helpful?