Dawit Getnet
Dawit Getnet
Out put
1
2. Write C# Program to print the elements of an array in reverse
order?
using System;
{
public static void Main()
{
int[] arr = new int[] { 5, 6, 7, 8, 9, 10 };
Console.WriteLine();
}
Console.ReadLine();
}
}
Out put
2
3. Write C# Program to print the largest element present in an array?
using System;
{
public static void Main()
{
max = arr[i];
}
Console.WriteLine("Largest element present in given array: " + max);
Console.ReadLine();
}
}
Out put
3
4. Write C# Program to print the sum of all the elements of an array?
using System;
{
public static void Main()
{
int[] arr = new int[] { 10, 5, 25, 5, 15 };
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
sum = sum + arr[i];
}
Console.WriteLine("Sum of all the elements of an array: " + sum);
Console.ReadLine();
}
Out put
4
Explain
Explain Exception in C#
Exceptions in C# are used to handle runtime errors in a program. When an exception occurs, the
normal flow of the program is disrupted, and the control is transferred to the nearest exception
handler. This mechanism helps in maintaining the stability and reliability of the program by
preventing it from crashing due to unexpected errors. In C#, exceptions are represented by
objects that are derived from the System. Developers can create custom exception classes by
deriving from the Exception class to handle specific types of errors in their applications. This
allows for better organization and management of different types of exceptions that may occur
during the execution of the program. Handling exceptions in C# is done using try-catch blocks.
The code that might throw an exception is placed inside the try block, and the catch block is used
to handle the exception if it occurs. This way, developers can gracefully recover from errors and
take appropriate actions, such as logging the error, displaying a message to the user, or
performing cleanup operations before the program terminates.
An exception in C# represents an error condition that arises during the execution of a program.
Exceptions can occur due to various reasons, such as incorrect input, resource unavailability, or
logical errors in the code. The .NET runtime throwing exceptions is a way of informing the
programmer about these errors.
When an exception is thrown, the default behavior is to terminate the program, and the content of
the stack trace is displayed to the console. However, a programmer can catch exceptions and
handle them according to their requirements.
In C#, there are built-in exception classes that represent specific error conditions. Some common
exceptions are Null Reference Exception, Index out of Range Exception, and Divide By Zero
Exception. Custom exceptions can also be created by inheriting from the Exception class.
When an exception is thrown, the program starts looking for a suitable catch block that can
handle the exception. The first catch block that can handle the type of exception thrown is used
to execute the error handling code. If no suitable catch block is found, the program will
terminate. This mechanism allows for the differentiation of various error conditions and provides
a way to handle them appropriately.
5
even after runtime errors. In C#, exception is an event or object which is
thrown at runtime. All exceptions the derived from System. It is a runtime
error which can be handled. If we don't handle the exception, it prints
exception message and terminates the program.
To handle exceptions, developers can use try-catch blocks. The try block
contains the code that might throw an exception, while the catch block is
used to handle the exception if it occurs.
6
Define custom exceptions with example
Custom exceptions in programming, often used in object-oriented languages like Java or
Python, are exceptions that you define yourself to handle specific error conditions in your code.
By creating custom exceptions, you can provide more meaningful error messages and improve
the readability and maintainability of your code. They allow you to differentiate between
different types of errors and handle them appropriately within your program.
Custom exceptions in C# are user-defined exceptions that allow developers to create their own
exception types to handle specific error conditions in their code. By creating custom exceptions,
developers can provide more detailed information about the error and handle it in a more
specific way. This can help improve the overall error handling and debugging process in C#
applications. Let me know if you need more information on this topic!
Custom exceptions provide a way to signal unique error conditions and react to them in a
structured manner.
C# includes the built-in exception types such as Null Reference Exception, Memory Overflow
Exception, etc. However, you often like to raise an exception when the business rule of your
application gets violated. So, for this, you can create a custom exception class by deriving the
Application Exception class.
using System;
public class ExExample
{
public static void Main(string[] args)
{
int a = 10;
int b = 0;
int x = a/b;
Console.WriteLine("Rest of the code");
Console.ReadLine();
}
7
}
To throw an exception in C#, you use the throw keyword followed by an instance of an
exception class. For example:
This will create and throw a new instance of the Exception class with the specified error
message. You can also throw specific types of exceptions, such as Argument Exception or File
Not Found Exception, depending on the nature of the error.
8
Throwing exceptions with example
using System;
public class DivZero
{
static public void Main ()
{
// Set an integer equal to 0
int IntVal1 = 0;
// and another not equal to zero
int IntVal2 = 57;
try
{
Console.WriteLine ("{0} / {1} = {2}", IntVal2, IntVal1, IntResult (IntVal2,
IntVal1) / IntResult (IntVal2, IntVal1));
}
catch (DivideByZeroException e)
{
Console.WriteLine (e.Message);
}
// Set a double equal to 0
double dVal1 = 0.0;
double dVal2 = 57.3;
try
{
Console.WriteLine ("{0} / {1} = {2}", dVal2, dVal1, DoubleResult (dVal2, dVal1));
}
catch (DivideByZeroException e)
{
Console.WriteLine (e.Message);
}
}
static public int IntResult (int num, int denom)
{
return (num / denom);
}
static public double DoubleResult (double num, double denom)
{
return (num / denom);
}
}
}