Thursday, April 14, 2016

Additional Resources



I have had requests to provide any online resources to supplement the course work and class time.


Below are a few links I think you will find useful:


http://csharp.net-tutorials.com/

Welcome to this C# Tutorial, currently consisting of 49 articles covering all the most important C# concepts. This tutorial is primarily for new users of this great technology, and we recommend you to go through all the chapters, to get the most out of it as possible. While each chapter can be used without reading the previous chapters, some of them may reference things done in earlier chapters.


OOP Related Links:


http://csharp.net-tutorials.com/classes/abstract-classes/
http://csharp.net-tutorials.com/classes/inheritance/
http://csharp.net-tutorials.com/classes/interfaces/




http://www.csharp-station.com/Tutorial/CSharp/SmartConsoleSetup.aspx


Welcome to the C# Station Tutorial This is a set of lessons suited for beginning to intermediate programmers or anyone who would like to gain familiarity with the C# programming language. These lessons will help you get a quick head-start with C# programming.



http://www.c-sharpcorner.com/UploadFile/mkagrahari/introduction-to-object-oriented-programming-concepts-in-C-Sharp/

Introduction to Object Oriented Programming (OOP) concepts in C#: Abstraction, Encapsulation, Inheritance and Polymorphism.







Tuesday, April 12, 2016

Lesson 12

Chapter 13

Concepts: 
Exception handling
Specialized exception types (Known Exceptions)
Try Catch Finally

Exception Handling

Chapter 13


The C# language's exception handling features help you deal with any unexpected or exceptional situations that occur when a program is running. Exception handling uses the try, catch, and finally keywords to try actions that may not succeed, to handle failures when you decide that it is reasonable to do so, and to clean up resources afterward. Exceptions can be generated by the common language runtime (CLR), by the .NET Framework or any third-party libraries, or by application code. Exceptions are created by using the throw keyword.

In many cases, an exception may be thrown not by a method that your code has called directly, but by another method further down in the call stack. When this happens, the CLR will unwind the stack, looking for a method with a catch block for the specific exception type, and it will execute the first such catch block that if finds. If it finds no appropriate catch block anywhere in the call stack, it will terminate the process and display a message to the user

The basic exception handling model looks like this:

        private void DoSomething()
        {
            try
            {
            }
            catch (Exception)
            {
               
                throw;
            }
        }

We would first want to implement a couple of changes to the exception template code.  In most cases, we would need to assign a variable to the exception object so we can reference it in the exception block.

catch (Exception e)
{
              throw;
}

Using the variable "e" (commonly used to represent the exception object). Allows us to access the properties of the exception object when an exception is thrown in our code.

Typically we only want the message value but we can get the complete exception via the ToString() method.

catch (Exception e)
{
              MessageBox.Show(e.Message); //only the specific error message
              MessageBox.Show(e.ToString()); //the complete exception detail
}

Exceptions can be re-thrown. When a captured exception occurs we can either handle it or pass is back up the call stack. That is back up to the method or property that called it.

catch (Exception e)
{
              throw e;
}

The above code will take the exception and pass it back up to the calling method or property.

Known Exceptions

If you know in advance possible exceptions to your process we can implement exception handling to address specific issues

public static double DivideTwoNumber(double Value1, double Value2)
{
    try
    {
           return Value1 / Value2;
    }
    catch (DivideByZeroException e)
    {
          //Value 2 might be zero so we can trap for that possibility
          Console.WriteLine("Can't divide by zero");
          return 0;
    }
    catch (Exception e)
    {
          //If an exception occurs that is not a divide by zero exception then it is caught here
          throw e;
    }
}

In the method above we are going to capture two kinds of exceptions. If the method throws a divide by zero exception the first exception section would catch and process the exception. If the exception is not that then the second method would capture the exception. It is important to note that this kind of exception chaining should be done from the specific exception types to the more general. If the exception blocks in the code were reversed the divide exception would never be reached because the general exception block would catch them all.