C# exception handling ⚠️

Sdílet
Vložit
  • čas přidán 15. 06. 2024
  • C# exception handling tutorial example explained
    #C# #exception #handling
    using System;
    namespace MyFirstProgram
    {
    class Program
    {
    static void Main(string[] args)
    {
    // exception = errors that occur during execution
    // try = try some code that is considered "dangerous"
    // catch = catches and handles exceptions when they occur
    // finally = always executes regardless if exception is caught or not
    int x;
    int y;
    double result;
    try
    {
    Console.Write("Enter number 1: ");
    x = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter number 2: ");
    y = Convert.ToInt32(Console.ReadLine());
    result = x / y;
    Console.WriteLine("result: " + result);
    }
    catch (FormatException e)
    {
    Console.WriteLine("Enter ONLY numbers PLEASE!");
    }
    catch (DivideByZeroException e)
    {
    Console.WriteLine("You can't divide by zero! IDIOT!");
    }
    catch (Exception e)
    {
    Console.WriteLine("Something went wrong!");
    }
    finally
    {
    Console.WriteLine("Thanks for visiting!");
    }
    Console.ReadKey();
    }
    }
    }
  • Věda a technologie

Komentáře • 41

  • @BroCodez
    @BroCodez  Před 3 lety +15

    using System;
    namespace MyFirstProgram
    {
    class Program
    {
    static void Main(string[] args)
    {
    // exception = errors that occur during execution
    // try = try some code that is considered "dangerous"
    // catch = catches and handles exceptions when they occur
    // finally = always executes regardless if exception is caught or not
    int x;
    int y;
    double result;
    try
    {
    Console.Write("Enter number 1: ");
    x = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter number 2: ");
    y = Convert.ToInt32(Console.ReadLine());
    result = x / y;
    Console.WriteLine("result: " + result);
    }
    catch (FormatException e)
    {
    Console.WriteLine("Enter ONLY numbers PLEASE!");
    }
    catch (DivideByZeroException e)
    {
    Console.WriteLine("You can't divide by zero! IDIOT!");
    }
    catch (Exception e)
    {
    Console.WriteLine("Something went wrong!");
    }
    finally
    {
    Console.WriteLine("Thanks for visiting!");
    }
    Console.ReadKey();
    }
    }
    }

  • @housseinhoussein4575
    @housseinhoussein4575 Před 7 měsíci +9

    bro i watched your code since 3 year ago and i will be graduated soon you helped me so much in the university thanks so much

  • @kylefreeman8084
    @kylefreeman8084 Před rokem +10

    "Not considered good practice" Someone should tell Microsoft, omfg.

  • @dumbbo123
    @dumbbo123 Před 11 měsíci +5

    one point is that we can also make use of e.Message to show what the actual exception error occurred. awesome video bro code🙂

  • @25dall
    @25dall Před 3 měsíci

    Think you are gonna help me alot in my education - thanks for the vid ::)

  • @aballahsameh3597
    @aballahsameh3597 Před 2 lety +2

    you'r so good, BRO!

  • @spartanranger
    @spartanranger Před 2 lety +3

    Thanks for the video Bro.

  • @annie.araujo
    @annie.araujo Před rokem

    Thanks for the video!

  •  Před 2 lety +1

    Thanks Bro!

  • @user-kb3un1dx7u
    @user-kb3un1dx7u Před 3 měsíci

    Superb broski
    !

  • @youcefmantas4944
    @youcefmantas4944 Před rokem

    Thanks For The Video Mate

  • @mherman90
    @mherman90 Před rokem +14

    Bro, what about using the general Exception catch all and then you can use e.Message and do something like this:
    catch (Exception e)
    {
    Console.WriteLine("something went wrong: " + e.Message);
    }
    and you will have one exception writing out the specifics in the Message property.

  • @mansokleaksa1787
    @mansokleaksa1787 Před 2 lety

    Thanks Bro🤗

  • @nouchance
    @nouchance Před rokem

    Amazing SIR!

  • @BantuBeya
    @BantuBeya Před měsícem

    You're the real Bro, Bro.👌👌

  • @wayasho5284
    @wayasho5284 Před rokem

    thanks, bro.

  • @natnaelgetasew7250
    @natnaelgetasew7250 Před 2 lety

    Thanks bro

  • @spartanranger
    @spartanranger Před 2 lety +6

    Oh btw is there reason why we typed "e" after exceptions ? I tried without using it and looks like program works perfectly fine.

    • @saikokamiyt
      @saikokamiyt Před rokem +3

      I think you will already know it but if someone does not: You can add the e to your writeline Console.WriteLine($"Error: {e}"); to see what went wrong.

  • @rahellebey1009
    @rahellebey1009 Před 3 měsíci

    👏🙏👌

  • @TylerHerwek
    @TylerHerwek Před rokem

  • @LucParent905
    @LucParent905 Před 2 lety +1

    I like you doggo

  • @whitedinamo
    @whitedinamo Před 2 lety

    lesson check😇

  • @ahmedhassan5783
    @ahmedhassan5783 Před 6 měsíci

    done

  • @budderrar5751
    @budderrar5751 Před rokem

    noice

  • @mmisterWhite
    @mmisterWhite Před 8 měsíci

    If you write good code you should not have any exceptions right? I just made my code so that there will never be exceptions if you try to do something wrobg 😅

  • @user-xx8mw2kq7t
    @user-xx8mw2kq7t Před 6 měsíci

    whats the e for?

  • @mukesh6205
    @mukesh6205 Před 8 měsíci

    What we can do to run from the beginning after catch the exception?

    • @OjiniKinlongi
      @OjiniKinlongi Před 5 měsíci

      I used this for it
      int number;
      int number2;
      int result;
      string re;
      bool reset = true;
      while (reset) {
      try
      {
      Console.WriteLine("Enter the number one!");
      number = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Enter the number one!");
      number2 = Convert.ToInt32(Console.ReadLine());
      result = number + number2;
      Console.WriteLine(result); ;
      }
      catch (FormatException e)
      {
      Console.WriteLine("Wrong input! " + e.Message);
      Console.WriteLine("Would you like to reset ? (Yes/No)");
      re = Console.ReadLine().ToLower();
      if (re == "yes")
      {
      reset = true;
      }
      else
      {
      Console.WriteLine("Thanks for using!");
      reset = false;
      }
      }
      }

  • @villocity5794
    @villocity5794 Před 11 měsíci

    If an exception is caught and fixed how do I make the code try again from were it last was?

    • @mahersabit9265
      @mahersabit9265 Před 9 měsíci

      maybe you can use an if statement? idk

    • @OjiniKinlongi
      @OjiniKinlongi Před 3 měsíci

      you can use bool date type and a loop to ensure the variables were taken in a correct format like this for example -
      int number1;
      int number2;
      bool success = false;
      while (!success) {
      try
      {
      Console.WriteLine("Enter number one");
      number1 = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine();
      Console.WriteLine("Enter number two");
      number2 = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine();
      Console.WriteLine($"Your total is {number1 + number2}");
      success = true;
      }catch (Exception)
      {
      Console.WriteLine("Please enter an integer");
      }
      }

    • @OjiniKinlongi
      @OjiniKinlongi Před 3 měsíci

      you can use bool date type and a loop to ensure the variables were taken in a correct format like this for example -
      int number1;
      int number2;
      bool success = false;
      while (!success) {
      try
      {
      Console.WriteLine("Enter number one");
      number1 = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine();
      Console.WriteLine("Enter number two");
      number2 = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine();
      Console.WriteLine($"Your total is {number1 + number2}");
      success = true;
      }catch (Exception)
      {
      Console.WriteLine("Please enter an integer");
      }
      }

  • @ibnOrfi
    @ibnOrfi Před 2 lety

    We can also use goto statement right? or is it not recommended? which method is better for error handling?

    • @edems131
      @edems131 Před rokem

      Tip for you: .bat sintax is bad and i dont know about good use of goto in C#

    • @baconmanthelegend
      @baconmanthelegend Před rokem

      It’s not recommended

  • @Alana18299
    @Alana18299 Před 6 měsíci

    bro really just sum up 40 min video from my prof in 5 mins.

  • @the_dude_josh
    @the_dude_josh Před 5 měsíci

    Random commend down below.

  • @SlimyMcTee
    @SlimyMcTee Před rokem

    How to prevent the exception from happening at all? So instead of just stopping the program, it looped back to re asking the number. Something like:
    while (age != 'a number or something') //something that telling age must be a number otherwise it looped back
    {
    Console.WriteLine("Not a number dummy, type again!")
    age = Convert.ToInt32(Console.ReadLine());
    }
    Is that even possible?

    • @crt24501
      @crt24501 Před rokem +1

      At the end of the catch, write "goto" + some name like "start again", and then just write "name you chose" and colon. When the catch is executed, and thus the error happens, it will send the code to that location.

    • @baconmanthelegend
      @baconmanthelegend Před rokem

      @@crt24501 that’s bad practice