In today's tutorial, we will see what an exception is and how to work with them.
1 - What is an exception?
An exception is a problem or error that arises suddenly in our code while it is running.
We need to handle exceptions, because if we don't, the program will stop working, which means its operation will cease.
The best way to avoid exceptions is to make sure they can't happen. For example, an exception can occur when we try to read a file and that file doesn't exist. So, to avoid the exception, we should check if the file exists, and if it does, read it. Otherwise, we shouldn't read the file.
But unfortunately, we can't always put a check or filter on every aspect of the program. So, what we need is a try-catch
block.
2 - Try-catch block
C# lets us handle exceptions using a try-catch
block. It's very simple, we put code inside the try
block, and if an exception is thrown, the catch
block will execute, which contains another block of code. This way, we prevent the program from stopping.
For example, we can't divide a number by 0, which causes an exception.
try
{
int numero = Convert.ToInt32(Console.ReadLine());
decimal division = 25 / numero;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
As we can see, the catch
block gives us access to the Exception
variable, which contains all the information about the exception like the message or the stacktrace, which tells us where the exception occurred.
Another very common option inside a catch is to use the throw clause, which will send the exception to the parent catch block.
try
{
decimal division = 25 / numero;
}
catch (Exception ex)
{
throw;
}
This means that if the method that throws the exception is handled at a higher level, it will execute the catch at the higher level. If not, the execution of the program will stop.
3 - Specify an exception
As we saw, this exception is very specific, since you can't divide a number by zero.
Inside a try-catch
block, we can indicate as many catch
blocks as we want, as long as the data type (Exception
) is different. As we saw with constructors, here we also use operator overloading.
For our example, another possibility is that the number we enter is a word and not a number. If we want to check for that specific exception, we should specify it in the catch block as shown below.
Console.WriteLine("Introduce un numero");
try
{
int numero = Convert.ToInt32(Console.ReadLine());
decimal division = 25 / numero;
}catch (FormatException ex)
{
Console.WriteLine("El valor introducido no era un numero entero");
}
catch(DivideByZeroException ex)
{
Console.WriteLine("No es posible dividir por 0");
}
catch (Exception ex)
{
throw;
}
As we can see, if the value entered is not a number, the execution will continue in catch(FormatException)
. If that value is 0, it will continue in catch(DivideByZeroException)
. And if the exception is any other, it will jump to catch(Exception)
, which is the default block.
4 - Conclusion
Exception handling is crucial in our daily work, so we must keep it in mind when working on our personal projects, or while studying, since it will make the system more robust against failures and critical issues.
If there is any problem you can add a comment bellow or contact me in the website's contact form