USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
Exception handling
1.
2. What is exception? Exceptions are unusual error conditions that occur during execution of a program or an application. In C# exception handling is achieved using the try ? catch ? finally block. All the three are C# keywords that are used do exception handling. The try block encloses the statements that might throw an exception where as catch block handles any exception if one exists. The finally block can be used for doing any clean up process. try { // Statements that are can cause exception } catch(Type x) { // Statements to handle exception } finally { // Statement to clean up }
3.
4.
5.
6.
7. Showing an Error Message to User This kind of Exception Handling mechanism is the most commonly used mechanism. try { // Code where we are anticipating the Exception } catch (Exception) { System.Windows.Forms.MessageBox.Show("Error Message: This is the Message shown to the User"); } The message shown to the user can be an error message, warning message or an Information message.
8. What are Custom Exceptions? Why do we need them? Custom Exceptions are exceptions that are created for applying the Business rules of the Application. These exceptions are used to implement the business rules violations of the application. For example: The minimum balance set for Savings A/C in a bank would be different from a Current A/C. Hence, while developing the application for the same the Business rule would be kept as the same. Violation of Business Rule: Whenever a Violation of Business rule is done, it should be notified and the proper message should be shown to the user. This will be handled by a Custom Exception. Let us illustrate the same with an example.
9. private void ValidateBeforeDebit(float Amount) { { if((Balance – Amount ) < MimimumBalanceNeedToHave) { throw new MimimumBalanceViolationForSavingsAccount("Current TransactionFailed: Minimum Balance not available"); } Else { Debit(Amount); } } } We have a method ValidateBeforeDebit (). This method ensures the minimum balance. If there is no minimum balance then a Custom Exception of the type MimimumBalanceViolationForSavingsAccount is thrown. We need to handle the method whenever we call the method.
10.
11. Example of handled exception: The above program can be modified in the following manner to track the exception. You can see the usage of standard DivideByZeroException class using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine(?Program terminated before this statement?); } catch(DivideByZeroException de) { Console.WriteLine("Division by Zero occurs"); } finally { Console.WriteLine("Result is {0}", intTemp); } } }
12. In the above example the program ends in an expected way instead of program termination. At the time of exception program control passes from exception point inside the try block and enters catch blocks. As the finally block is present, so the statements inside final block are executed. Example of exception un handled in catch block As in C#, the catch block is optional. The following program is perfectly legal in C#. using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine("Not executed line"); } finally { Console.WriteLine("Inside Finally Block"); } Console.WriteLine("Result is {0}", intTemp); } }
13. The above example will produce no exception at the compilation time but the program will terminate due to un handled exception. But the thing that is of great interest to all the is that before the termination of the program the final block is executed. Example of Multiple Catch Blocks: A try block can throw multiple exceptions, that can handle by using multiple catch blocks. Important point here is that more specialized catch block should come before a generalized one. Otherwise the compiler will show a compilation error. //C#: Exception Handling: Multiple catch
14. using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine("Not executed line"); } catch(DivideByZeroException de) { Console.WriteLine("DivideByZeroException" ); } catch(Exception ee) { Console.WriteLine("Exception" ); }
15. finally { Console.WriteLine("Finally Block"); } Console.WriteLine("Result is {0}", intTemp); } } Example of Catching all Exceptions By providing a catch block without a brackets or arguments, we can catch all exceptions occurred inside a try block. Even we can use a catch block with an Exception type parameter to catch all exceptions happened inside the try block since in C#, all exceptions are directly or indirectly inherited from the Exception class. //C#: Exception Handling: Handling all exceptions
16. using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine("Not executed line"); } catch { Console.WriteLine("oException" ); } Console.WriteLine("Result is {0}", intTemp); } }
17. The following program handles all exception with Exception object. //C#: Exception Handling: Handling all exceptions using System; class HandledException { public static void Main() { int x = 0; int intTemp = 0; try { intTemp = 100/x; Console.WriteLine("Not executed line"); } catch(Exception e) { Console.WriteLine("oException" ); } Console.WriteLine("Result is {0}", intTemp); } }
18. Example of Throwing an Exception: C# provides facility to throw an exception programmatically. The 'throw' keyword is used for doing the same. The general form of throwing an exception is as follows. For example the following statement throw an ArgumentException explicitly. throw new ArgumentException("Exception"); using System; class HandledException { public static void Main() { try { throw new DivideByZeroException("Invalid Division"); } catch(DivideByZeroException e) { Console.WriteLine("Exception" ); } Console.WriteLine("Final Statement that is executed"); } }
19.
20. User-defined Exceptions: C#, allows to create user defined exception class this class should be derived from Exception base class. So the user-defined exception classes must inherit from either Exception class or one of its standard derived classes. //C#: Exception Handling: User defined exceptions using System; class UserDefinedException : Exception { public MyException(string str) { Console.WriteLine("User defined exception"); } } class HandledException { public static void Main() { try { throw new UserDefinedException ("New User Defined Exception"); }
21. catch(Exception e) { Console.WriteLine("Exception caught here" + e.ToString()); } Console.WriteLine("Final Statement that is executed "); } }