What is exception handling in c3?

handling is used to detect and handle these runtime errors. If there is no exception handling mechanism created by the programmer, then the default mechanism is used by the runtime environment. This terminates the program execution.

Exception Handling Keywords

The exception handling mechanism uses three main keywords i.e. try, catch and finally. Details about these are given as follows:

1. try

If the exception occurs inside the try block, the control is transferred to the required catch block. There may be multiple catch blocks after the try block.

2. catch

The catch block handles the exception that has occured in an appropriate manner using exception handling mechanisms.

3. finally 

The finally block executes some statements irrespective of whether an exception has occurred or not. If there is no exception in the try block, then control directly transfers to finally block and skips the catch block.

The syntax of the try, catch and finally keywords in exception handling is given as follows:

try
{
    // Statements that cause exception
}
catch (ExceptionType EName)
{
    // Statements that handle exception
}
finally
{
     // Remaining statements to be executed
}

In the above syntax, the try block contains the exceptions that may occur. There can be multiple catch blocks that handle the required exceptions. The finally block executes some remaining statements.

A program that demonstrates exception handling is given as follows:

Source Code: Program that demonstrates exception handling in C#

using System;
namespace ErrorHandlingDemo
{
  class Example
  {
     static void Main(string[] args)
     {
         int result = 0;
         int a = 25;
         int b = 0;
         try
        {
           result = a / b;
        }
        catch (DivideByZeroException)
        {
           Console.WriteLine("Exception occured");
        }
        finally
        {
           Console.WriteLine("Result: {0}", result);
        }
     }
  }
}

The output of the above program is as follows:

Exception occured
Result: 0

Now let us understand the above program.

The try block stores the result of a/b. If b is 0, then there is a DivideByZeroException and control goes to the catch block. In the catch block, it is printed that an exception has occurred. The finally block is executed if there is an exception or not. It prints the result of a/b. The code snippet for this is as follows:

int result = 0;
         int a = 25;
         int b = 0;
         try
        {
           result = a / b;
        }
        catch (DivideByZeroException)
        {
           Console.WriteLine("Exception occured");
        }
        finally
        {
           Console.WriteLine("Result: {0}", result);
        }

User Defined Exceptions

User defined exceptions are custom exceptions that can be created by the users. This is done by inheriting the exception class.

A program that demonstrates user defined exceptions is given as follows:

Source Code: Program that demonstrates user defined exceptions in C#

using System;
namespace UserDefinedExceptionDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Age a = new Age();
        try
        {
           a.displayAge();
        }
        catch(AgeIsNegativeException e)
        {
           Console.WriteLine("AgeIsNegativeException: {0}", e.Message);
        }
     }
  }
}
public class AgeIsNegativeException: Exception
{
  public AgeIsNegativeException(string message): base(message)
  {
  }
}
public class Age
{
  int age = -5;
  public void displayAge()
  {
     if(age < 0)
     {
        throw (new AgeIsNegativeException("Age cannot be negative"));
     }
     else
     {
        Console.WriteLine("Age is: {0}", age);
     }
  }
}

The output of the above program is as follows:

AgeIsNegativeException: Age cannot be negative

Now let us understand the above program.

The try and catch blocks are available in the Main() function. The displayAge() function is called in the try block. If the age is less than zero, then an exception message is printed in the catch block because the age cannot be less than zero. The code snippet for this is as follows:

 static void Main(string[] args)
     {
        Age a = new Age();
        try
        {
           a.displayAge();
        }
        catch(AgeIsNegativeException e)
        {
           Console.WriteLine("AgeIsNegativeException: {0}", e.Message);
        }
     }

The variable of type int is declared in class Age. The function displayAge() throws an exception if the age is less than zero. Otherwise, it prints the age. The code snippet for this is as follows:

public class Age
{
  int age = -5;
  public void displayAge()
  {
     if(age < 0)
     {
        throw (new AgeIsNegativeException("Age cannot be negative"));
     }
     else
     {
        Console.WriteLine("Age is: {0}", age);
     }
  }
}

What is exception c3?

An exception is defined as an event that occurs during the execution of a program that is unexpected by the program code. The actions to be performed in case of occurrence of an exception is not known to the program. In such a case, we create an exception object and call the exception handler code.

What is exception handling explain with example?

Examples include a user providing abnormal input, a file system error being encountered when trying to read or write a file, or a program attempting to divide by zero. Exception handling attempts to gracefully handle these situations so that a program (or worse, an entire system) does not crash.

What is the exception handling procedure?

Exception handling is the process of: Examining an exception message which has been issued as a result of a run-time error. Optionally modifying the exception to show that it has been received (that is, handled)

What are the types of exceptions in C#?

Exception and SystemException..
ApplicationException..
InvalidOperationException..
ArgumentException, ArgumentNullException, and ArgumentOutOfRangeException..
NullReferenceException, IndexOutOfRangeException, and AccessViolationException..
StackOverflowException..
OutOfMemoryException..