03_Handout_1_unlocked.txt

Full Transcript

Exception Handling The Exception Hierarchy Exception is represented by classes. All the exceptions are subclasses in the built-in exception class named Exception, wherein it is a part of namespace System. Two (2) Types of Exceptions o ApplicationException - These exceptions are user program-gen...

Exception Handling The Exception Hierarchy Exception is represented by classes. All the exceptions are subclasses in the built-in exception class named Exception, wherein it is a part of namespace System. Two (2) Types of Exceptions o ApplicationException - These exceptions are user program-generated. o SystemException - These exceptions are generated by Common Language Runtime (CLR). Here are the few exceptions which are commonly used: * System.Exception - This is at the top of the standards' exceptions hierarchy. The runtime system in C# generates all the exceptions. * System.ArithmeticException - Errors in arithmetic or conversion operation will be thrown in this exception. * System.OverflowException - When an overflow occurs in a checked operation, it will be thrown in OverflowException. * System.ArgumentException - Any invalid argument in a method will be thrown in this exception. * System.ArgumentNullException - If there is an unacceptable argument passed to a method, it will be thrown in ArgumentNullException. * System.IndexOutOfRangeException - Throw in this exception when attempting to index an array through an index that is either less than zero or greater than the maximum length of index. * System.OutOfMemoryException - If the available memory becomes too low to accommodate a memory allocation request, it will be thrown in OutOfMemoryException. * System.StackOverflowException - The exception StackOverflowException is called when the execution stack is exhausted by having too many pending method calls. * System.FormatException - This exception checks the format of the string or argument if it is invalid. The try-catch Block Handling exceptions in C# uses four (4) keywords: * try - This keyword is used to check for the occurrence of any exceptions enclosed to it. * catch - This keyword catches the exception that is thrown on the occurrence of exception in a try block. * throw - It is used to throw an exception manually. * finally - This keyword executes a given statement even if the exception is thrown or not thrown. This block cannot transfer control by using break, continue, return, or goto. Throwing an Exception To catch exceptions, use the try-catch block to check the errors in the code and generate in the runtime system automatically. Manually throwing using the throw keyword can also be used. The syntax is throw new exception_Object; The exception_Object is an instance of a class derived from the Exception class. The new operator is used to create a new object. Listing 1. Using throw without the try-catch block Listing 1 shows how DivideByZeroException is thrown manually. The exception was caught in an if condition that validates the input using the new operator. If the condition is equal to 0, the thrown exception will be called. The throw keyword can be used outside of the try-catch block, but without the try-catch block, there will be an interruption in the process. This will show an error message to the console and the application will be closed. If throwing an exception, a catch block is needed. Listing 2. Using throw with try-catch block Listing 2 contains two (2) catch blocks to check the format and check if the entered number is zero. In contrast, Listing 1 only shows an exception that will be thrown and will not catch when the exception occurs, while Listing 2 demonstrates how a catch block catches a manually thrown exception. The DividebyZeroException is thrown by using throw keyword and caught in a catch block and display the message.

Use Quizgecko on...
Browser
Browser