C# exception handling ⚠️

preview_player
Показать описание
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();
}
}
}
Рекомендации по теме
Комментарии
Автор

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 =

Console.Write("Enter number 2: ");
y =

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

BroCodez
Автор

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

housseinhoussein
Автор

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

definitelynotchris
Автор

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

dumbbo
Автор

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

kylefreeman
Автор

This has been super helpful, what a legend!

Cybershroom
Автор

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

dall
Автор

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.

mherman
Автор

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

spartanranger
Автор

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 😅

mmisterWhite
Автор

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

mukesh
Автор

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

Blocky
Автор

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

villocity
Автор

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

ibnOrfi
Автор

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

Is that even possible?

SlimyMcTee