Question

In: Computer Science

in C#, What is an exception and what are the advantages of exception handling?

in C#, What is an exception and what are the advantages of exception handling? What are the differences between the traditional error-handling methods and the object-oriented exception-handling methods and when should you use each one? Provide three to four paragraphs detailing your findings/views on these questions..provide examples with a simple code also..

Solutions

Expert Solution

What is an exception in c# ?

An exception will be a situation that arises during the execution of a program. A C# exception will be a response to such a situation . It will arise when the program is running. Ex,divide by zero.

When an exception arise we will jump to some other part of the code and then handle it.

Basically there are 4 blocks that are present to handle the exception. They try, catch, throw, finally.

try : It is a block which can throw an exception when the program is in the execution. The code is tried for the presence of exception hence the name "try".

catch : It is the block that catches exception and branches the program to a different part of code where the exception is handled.

throw : When the problem comes up, this part of the program throws up exception.

finally : These are the set of exceptions that will be executed irrespective of whether there is an exception or not.

What are the advantages of exception handling in c# ?

  1. Management of errors becomes very easy.
  2. separating the error handling code with regular code.
  3. grouping error types and error differentiation.

Differences in traditional error-handling methods and the object-oriented exception-handling methods ? Which one to use ?

The traditional error handling method is actually not any method at all. It only has the throw statement that throws the exception whenever it arises to handle it the way it should be handled. The throw statement branches to the catch statement. In order for any catch clause to pass control to the next catch on the stack, it must end by issuing a new throw statement.

Exceptions can also be considered as the classes as in object oriented programming. There are many exception classes in c# library. Prefer predefined exception classes instead of programming your own exception classes. An exception is seen as an object. All the relevant information regarding an exception is encapsulated in the object. These errors are classified and has a class hierarchy. They have their own methods of handling the errors. Hence because of this fact one should use the object oriented error handling methods because they are already present and one need not waste his own time on that.


Related Solutions

write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry by a user. a. Write a program that prompts a user to enter a length in feet and inches. The length values must be positive integers. b. Calculate and output the equivalent measurement in centimeters 1 inch = 2.54 centimeters c. Write the code to handle the following exceptions: If the user enters a negative number, throw and catch an error that gives...
What advantages does object-oriented exception handling provide? When a program contains multiple catch blocks, how are...
What advantages does object-oriented exception handling provide? When a program contains multiple catch blocks, how are they handled? Write the statement to declare a three-by-four array of integers with the elements initialized to zero. Name the array myArray. What is the difference between volatile and nonvolatile storage in Java programming? Give examples of different storage situations. What are some of the advantages of the ArrayList class over the Arrays class? How can you use the enhanced for loop? What does...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
In Python This assignment involves the use of text files, lists, and exception handling and is...
In Python This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>>   Enter gender...
This assignment involves the use of text files, lists, and exception handling and is a continuation...
This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>> Enter gender (boy/girl): boy...
Function Template and Exception Handling In part one, you are going to implement the selection sort...
Function Template and Exception Handling In part one, you are going to implement the selection sort function that is capable of sorting vectors of int, double or string. In part two you will be writing a try catch block to catch the out-of-range exception. You are to write three functions and manipulate main() function for this lab all of which should be written in one main.cpp file: Part one: unsigned min_index(const vector<T> &vals, unsigned index): Passes in an index of...
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes...
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes (Session and Problems) and one driver class and ensure the user inputs cannot cause the system to fail by using exception handling. Overview from Project . Implement a Java program that creates math flashcards for elementary grade students. User will enter his/her name, the type (+,-, *, /), the range of the factors to be used in the problems, and the number of problems...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT