16.11. Object Orientation in C#: Exception Handling

Página 27

Object orientation in C# is a programming approach that involves creating objects that contain both data and methods. One of the most important parts of object orientation in C# is exception handling. Exceptions are events that occur during the execution of a program that interrupt the normal flow of the program.

In C#, exception handling is mainly done through the use of try, catch and finally blocks. The try block contains code that could potentially cause an exception. The catch block is used to handle the exception if it occurs. The finally block contains code that is always executed, regardless of whether an exception occurs or not.

Let's start with a simple example. Consider the following code:

try
{
    int x = 0;
    int y = 5 / x;
}
catch (DivideByZeroException e)
{
    Console.WriteLine(e.Message);
}
finally
{
    Console.WriteLine("Block finally executed.");
}

In this example, we are trying to divide a number by zero, which will result in a DivideByZeroException exception. When this exception is thrown, control is passed to the catch block, which prints the exception message. Then the finally block is executed, printing the message "Finally block executed".

It is important to note that the catch block will only be executed if the specified exception is thrown. If a different exception is thrown, the catch block will not be executed. For example, if we changed our code to the following:

try
{
    int[] array = new int[5];
    Console.WriteLine(array[5]);
}
catch (DivideByZeroException e)
{
    Console.WriteLine(e.Message);
}
finally
{
    Console.WriteLine("Block finally executed.");
}

In this case, an IndexOutOfRangeException exception will be thrown, as we are trying to access an array index that does not exist. However, since our catch block is only catching DivideByZeroException exceptions, this exception will not be caught and the program will stop. The finally block will still be executed, however.

To handle different types of exceptions, we can have multiple catch blocks. For example:

try
{
    // code that can throw exceptions
}
catch (DivideByZeroException e)
{
    Console.WriteLine("Divide by zero exception: " + e.Message);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Index out of range exception: " + e.Message);
}
finally
{
    Console.WriteLine("Block finally executed.");
}

In this case, if a DivideByZeroException or IndexOutOfRangeException exception is thrown, it will be caught and handled accordingly.

Also, we can have a catch block without specifying an exception type. This catch block will catch all exceptions that were not caught by the previous catch blocks. For example:

try
{
    // code that can throw exceptions
}
catch (DivideByZeroException e)
{
    Console.WriteLine("Divide by zero exception: " + e.Message);
}
catch (IndexOutOfRangeException e)
{
    Console.WriteLine("Index out of range exception: " + e.Message);
}
catch (Exception e)
{
    Console.WriteLine("Generic exception: " + e.Message);
}
finally
{
    Console.WriteLine("Block finally executed.");
}

In this case, if an exception other than DivideByZeroException or IndexOutOfRangeException is thrown, it will be caught by the generic catch block.

In summary, exception handling is an important part of object orientation in C#, as it allows us to deal with errors and exceptions in a controlled manner, preventing our program from stopping unexpectedly. By using try, catch, and finally blocks, we can ensure that our code is robust and reliable.

Now answer the exercise about the content:

What is the function of the catch block in exception handling in C#?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

2816.12. Object Orientation in C#: Collections in C#

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text