C# exception handling ⚠️

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ย. 2024

ความคิดเห็น • 50

  • @BroCodez
    @BroCodez  3 ปีที่แล้ว +20

    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();
    }
    }
    }

  • @definitelynotchris4776
    @definitelynotchris4776 หลายเดือนก่อน +4

    Did a better job at explaining this in a 5 minute video than my proffessor did in a whole lecture!

    • @VeraJune-d5y
      @VeraJune-d5y 13 วันที่ผ่านมา

      Same as you. I feel like my college was actually on TH-cam.

  • @housseinhoussein4575
    @housseinhoussein4575 ปีที่แล้ว +20

    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

  • @dumbbo123
    @dumbbo123 ปีที่แล้ว +6

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

  • @kylefreeman8084
    @kylefreeman8084 ปีที่แล้ว +21

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

  • @mherman90
    @mherman90 ปีที่แล้ว +15

    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.

  • @Cybershroom
    @Cybershroom 2 หลายเดือนก่อน

    This has been super helpful, what a legend!

  • @25dall
    @25dall 9 หลายเดือนก่อน

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

  • @muhammadtaha4208
    @muhammadtaha4208 4 ชั่วโมงที่ผ่านมา

    Good teaching, I have one question though if you could answer it, why do we have "finally" if it'll print "all ok" anyways, I thought it works as "return 0" means "no error found", but it's printing on anything, what's the use of it?

  • @aballahsameh3597
    @aballahsameh3597 2 ปีที่แล้ว +2

    you'r so good, BRO!

  • @spartanranger
    @spartanranger 3 ปีที่แล้ว +7

    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 2 ปีที่แล้ว +4

      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.

  • @spartanranger
    @spartanranger 3 ปีที่แล้ว +3

    Thanks for the video Bro.

  • @BantuBeya
    @BantuBeya 7 หลายเดือนก่อน

    You're the real Bro, Bro.👌👌

  • @EmmyCodes-d9n
    @EmmyCodes-d9n 8 หลายเดือนก่อน

    Superb broski
    !

  • @anniepunk
    @anniepunk ปีที่แล้ว

    Thanks for the video!

  •  3 ปีที่แล้ว +1

    Thanks Bro!

  • @youcefmantas4944
    @youcefmantas4944 ปีที่แล้ว

    Thanks For The Video Mate

  • @marine2oorah4u15
    @marine2oorah4u15 3 วันที่ผ่านมา

    in an exemption check
    when bro used exemption e, is is tied to checks specifically? why e? are there other things that can be used?

  • @simik4830
    @simik4830 หลายเดือนก่อน

    for the algorithm as always!

  • @nouchance
    @nouchance ปีที่แล้ว

    Amazing SIR!

  • @vishnumaya1930
    @vishnumaya1930 3 หลายเดือนก่อน

    Thanks

  • @LucParent905
    @LucParent905 3 ปีที่แล้ว +1

    I like you doggo

  • @wayasho5284
    @wayasho5284 2 ปีที่แล้ว

    thanks, bro.

  • @mansokleaksa1787
    @mansokleaksa1787 3 ปีที่แล้ว

    Thanks Bro🤗

  • @Blocky7352
    @Blocky7352 ปีที่แล้ว

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

  • @mmisterWhite
    @mmisterWhite ปีที่แล้ว

    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 😅

    • @farpurple
      @farpurple 20 วันที่ผ่านมา

      Larger your code, more unstable or unpredicted behaviours, eventually it will raise exceptions.
      But actually I never saw try {} catch () {} seriously used anywhere..
      It is usually just check with if or smth

  • @TylerHerwek
    @TylerHerwek ปีที่แล้ว

  • @natnaelgetasew7250
    @natnaelgetasew7250 2 ปีที่แล้ว

    Thanks bro

  • @rahellebey1009
    @rahellebey1009 8 หลายเดือนก่อน

    👏🙏👌

  • @Lulzsek
    @Lulzsek 4 หลายเดือนก่อน

    Gracias hermano Call of duty

  • @whitedinamo
    @whitedinamo 2 ปีที่แล้ว

    lesson check😇

  • @villocity5794
    @villocity5794 ปีที่แล้ว

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

    • @mahersabit9265
      @mahersabit9265 ปีที่แล้ว

      maybe you can use an if statement? idk

    • @OjiniKinlongi
      @OjiniKinlongi 9 หลายเดือนก่อน

      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 9 หลายเดือนก่อน

      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");
      }
      }

  • @mukesh6205
    @mukesh6205 ปีที่แล้ว

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

    • @OjiniKinlongi
      @OjiniKinlongi 10 หลายเดือนก่อน

      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;
      }
      }
      }

  • @budderrar5751
    @budderrar5751 ปีที่แล้ว

    noice

  • @RyanRajesh-p8r
    @RyanRajesh-p8r ปีที่แล้ว

    whats the e for?

  • @ahmedhassan5783
    @ahmedhassan5783 ปีที่แล้ว

    done

  • @ibnOrfi
    @ibnOrfi 2 ปีที่แล้ว

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

    • @edems131
      @edems131 2 ปีที่แล้ว

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

  • @the_dude_josh
    @the_dude_josh 11 หลายเดือนก่อน

    Random commend down below.

  • @Sorosh-l7z
    @Sorosh-l7z หลายเดือนก่อน

    Lol 😅

  • @SlimyMcTee
    @SlimyMcTee 2 ปีที่แล้ว

    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 ปีที่แล้ว +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.