Question: General Instructions This practical task introduces you to error handling and covers several most common .NET Framework exception types that you will likely encounter as



General Instructions This practical task introduces you to error handling and covers several most common .NET Framework exception types that you will likely encounter as soon as you start developing larger applications. Exceptions that you will meet in practice can be thrown either by the runtime or the application's code. The latter case represents a class of exceptions that are defined by the application's developer. Some exceptions can be caused by a faulty program code, while other exceptions can result from an incorrect input that has not been accounted for in the application's code. When any of these errors occur, the system catches the error and raises an exception. The process of generating and signalling an error is referred to as throwing an exception. This is done using the "throw' keyword followed by a new instance of a class derived (inherited) from System. Exception. There are standard exception types provided by the . NET framework that you can use rather than creating a custom exception. Remember that execution failure occurs whenever a program (or its part) cannot do what it was designed to do. For example, if the OpenFile method cannot return a file handler to the caller, it would be considered an execution failure. Exceptions are the primary means of reporting errors in applications. Therefore, you must adhere to a number of rules when you throw or handle exceptions. Here are several basic rules that you should follow and always think about them when you work on your application. - Report execution failures by throwing exceptions. - Do not use exceptions for the normal flow of control, if possible. Consider terminating the program by calling System . Environment. FailFast method instead of throwing an exception if your code encounters a situation where it is unsafe for further execution. Consider the performance implications of throwing exceptions. Throw rates above 100 per second are likely to noticeably impact the performance of most applications. - Do not have public members that can either throw or not based on some option. Do not have public members that return exceptions as the return value. - Do not throw exceptions from exception filter blocks [i.e. the "catch' block of the "try-catch' statement). - Avoid explicitly throwing exceptions from 'finally blocks. This list is by no means exhaustive and there are more rules and strategies. Remember that exception handling is an art which once you master grants you immense powers. 1. Start this practical task with watching lecture 3. In this task, you will need to conduct a small research about every exception presented in the following list: a) NullReferenceException () ArgumentNull Exception b) IndexOutOfRangeException g) ArgumentOutOfRangeException c) StackOverflowException h) FormatException d) OutOfMemoryException i) ArgumentException e) DivideByZero Exception SystemException In Section 9 of SIT232 Workbook, you will find a general information on how to catch and handle exceptions along with some details on the above exceptions. These exceptions are also briefly described and exemplified in the lecture notes for week 3 (you though are not allowed to copy the examples from the lecture into your solution). However, you will still need to do search in the Internet to gather more details about their usage. We also recommend you to refer to the .NET Framework referenceSIT232 Object-Oriented Development Trimester 2, 2023 documentation, which is rich of various examples. Finally, do not forget to look at the references at the bottom of this task. The expected solution for this task must consist of two parts: a program code that creates a situation that generates each of the above exceptions and a text report that explains in your own words the facts you found. As the result of your study, your report must answer the following questions and discuss the related issues for each of the above exceptions. - What is a possible situation that leads to the exception? Who is in charge of throwing the exception: you as a programmer or the runtime system? In theory, should you throw exceptions of this type? Explain your answer. - If you need to throw the exception, what details would you provide as a message to the user (caller)? What parameters would you include in the message? Can the exception be generally caught (and therefore handled)? - If the exception occurs, should you generally catch this exception type or is it better to pass such exception to the user (caller)? It is not enough to say yes or no; you must provide an argument to support your answer. - Is the exception a case when you want to avoid this exception to occur in your application in general? If so, what would be your actions as a programmer to avoid it? Note that you must check whether all the above questions are actually relevant to a particular exception that you consider. Remember that some exceptions are reserved and are to be thrown only by the runtime system; in most cases they need to indicate a serious runtime problem. Other exceptions can be programmer-defined and report about an incorrect input data or anticipated application failures. Thus, some exceptions you can avoid by doing a proper argument checking. So, to answer these questions correctly, you will have to focus on multiple implementation aspects and potential situations associated with each exception type. 2. The following is an illustrative example of how your solution may look like. Here, we discuss the InvalidOperationException type and give a sample code that throws it. This exception type is thrown when a method call is invalid for the object's current state. This, for example, can be caused by changing a collection (e.g., the content of an instance of the List
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
