βœ… Exceptions

Are exceptions essentially if statements that are global across the entire program?
14 Replies
Merineth πŸ‡ΈπŸ‡ͺ
Such as if I’m dividing by 0 somewhere, I can make an exception that handles that specific problem across my entire program?
Pobiega
Pobiegaβ€’2mo ago
uh Not as such, no an exception is an object that represents an error of some sort, and they "bubble up" until caught if you never catch it, it will terminate your program with an error are you familar with whats called "the call stack"?
Merineth πŸ‡ΈπŸ‡ͺ
Exceptions are β€˜β€™try’’ and β€˜β€™catch’’ right?
Pobiega
Pobiegaβ€’2mo ago
no those are keywords that relate to exceptions exceptions are C# objects that inherit from the Exception class
Merineth πŸ‡ΈπŸ‡ͺ
Ok so an exception class is a class that’s meant to catch possible problems?
Pobiega
Pobiegaβ€’2mo ago
no, it represents an instance of the problem hang on, let me write some demo code
Merineth πŸ‡ΈπŸ‡ͺ
So instead of crashing the entire program it will give an exception instead with try and catch?
Pobiega
Pobiegaβ€’2mo ago
try
{
// here is where we do dangerous stuff that might throw exceptions
var y = 0;
var x = 5 / y; // this will cause an error, specifically a `DivideByZeroException`
}
catch (DivideByZeroException ex)
{
// here we write code that can handle the particular exception
Console.WriteLine("You can't divide by zero!");
}
try
{
// here is where we do dangerous stuff that might throw exceptions
var y = 0;
var x = 5 / y; // this will cause an error, specifically a `DivideByZeroException`
}
catch (DivideByZeroException ex)
{
// here we write code that can handle the particular exception
Console.WriteLine("You can't divide by zero!");
}
Merineth πŸ‡ΈπŸ‡ͺ
So we have to know beforehand what kind of error will occur to catch it?
Buddy
Buddyβ€’2mo ago
I mean you can use
try
{

}
catch (Exception ex)
{

}
try
{

}
catch (Exception ex)
{

}
Which catches all exceptions, but generally not recommended. If you can try to avoid methods that can throw exceptions all-together
Merineth πŸ‡ΈπŸ‡ͺ
This does make senses tho Ok so it’s generally better to know what problem could occur to catch that specific problem? What would code look like if I want to catch multiple errors that could occur?
try
{
}
catch (Exception1, Exception2)
{
}
try
{
}
catch (Exception1, Exception2)
{
}
Like this?
Pobiega
Pobiegaβ€’2mo ago
try
{
// here is where we do dangerous stuff that might throw exceptions
var y = 0;
var x = 5 / y; // this will cause an error, specifically a `DivideByZeroException`
}
catch (DivideByZeroException ex)
{
Console.WriteLine("You can't divide by zero!");
}
catch (InvalidOperationException ex)
{
Console.WriteLine("An invalid operation was performed!");
}
try
{
// here is where we do dangerous stuff that might throw exceptions
var y = 0;
var x = 5 / y; // this will cause an error, specifically a `DivideByZeroException`
}
catch (DivideByZeroException ex)
{
Console.WriteLine("You can't divide by zero!");
}
catch (InvalidOperationException ex)
{
Console.WriteLine("An invalid operation was performed!");
}
like this its pretty rare that you catch two or more exceptions with the exact same fix and if you do, you can use...
catch (Exception ex) when (ex is DivideByZeroException or InvalidOperationException)
{
Console.WriteLine("You can't divide by zero or perform invalid operations!");
}
catch (Exception ex) when (ex is DivideByZeroException or InvalidOperationException)
{
Console.WriteLine("You can't divide by zero or perform invalid operations!");
}
but this is very rare, as said
Merineth πŸ‡ΈπŸ‡ͺ
Ok thanks I think I understand it now :)
Want results from more Discord servers?
Add your server