Exception Handling in C#
Exception Handling in C#
Exception Handling ||
: Exception handling
Ms. Nausheeda B S
Mahmoud Rafeek Alfarra
Contents
Exception handling
Syntax
Exception Classes in C#
Example
Practices
2
What is an exception error?
A C# exception is a response to an
exceptional situation that arises while a
program is running, such as an attempt to
divide by zero.
3
What is an exception error?
4
Exception handling
5
Exception handling
6
Syntax
7
Syntax
8
Syntax
9
Syntax
10
Hierarchy of Exceptions
11
Exception Classes in C#
12
Example 1
class Program {
public static void division(int num1, int num2)
{
float result=0.0f;
try
{
result = num1 / num2;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception Error !! \n divid by zero !!");
// Console.WriteLine("Exception caught: {0}", e);
}
finally
{
Console.WriteLine("Result: {0} ", result);
}
}
static void Main(string[] args)
{
division(10,0);
Console.ReadLine();
} } 13
Example 2
Use
IndexOutOfRangeException
&
FormatException
&
Exception
15
public class Student
{
Console.WriteLine(std.StudentName);
}
17
Throwing our own Exceptions
Examples:
throw new ArithmeticException();
throw new FormatException();
Every user
defined
exception New Exception
must be Class
defined as a
new class
19
User Defined Exception
using System;
namespace ExceptionHandling
{
class NegativeNumberException:Exception
{
public NegativeNumberException(string message)
// show message
}
}
if(value<0)
throw new NegativeNumberException(" Use Only Positive numbers");
catch(NegativeNumberException e)
{
}
20
Best Practices
21
Exceptions can decrease the application performance
◦ Throw exceptions only in situations which are really exceptional
and should be handled
◦ Do not throw exceptions in the normal program control flow
(e.g.: on invalid user input)
22
Write a C# program that prompts the user to input two numbers and
divides them. Handle an exception when the user enters non-numeric
values.
23
using System;
class Program
{
static void Main(String[] args) {
try {
Console.Write("Input the first number: ");
string inp1 = Console.ReadLine();
double number1 = Convert.ToDouble(inp1);
25
using System;
class Program {
static void ValidateNumber(int number) {
if (number < 0) {
throw new NegativeNumberException("Negative number not allowed.");
}
}
static void Main(String[] args) {
try {
Console.Write("Input an integer: ");
int number = Convert.ToInt32(Console.ReadLine());
ValidateNumber(number);
Console.WriteLine("Valid input: " + number);
}
catch (NegativeNumberException ex) {
Console.WriteLine("Error: " + ex.Message);
} 22
catch (FormatException) {
Console.WriteLine("Error: Invalid input. Please enter an integer.");
}
catch (Exception ex) {
Console.WriteLine("An error occurred: " + ex.Message);
}
27
using System;
class Program {
static void checkAge(int age)
{
if (age < 18) {
throw new ArithmeticException("Access denied, age must be 18 or more ");
}
else {
Console.WriteLine("Access granted - You are old enough!");
}
}
static void Main(string[] args) {
try {
int age;
Console.WriteLine("enter age");
age=Convert.ToInt32(Console.ReadLine());
checkAge(age);
} 24
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
29
Write a C# program that prompts the user to
input a numeric integer and throws an exception
if the number is less than 0 or greater than
1000.
30
using System;
class Program {
static void ValidateNumber(int number) {
if (number < 0 || number>1000) {
throw new NumberOutofRangeException("Number between 0 and 1000.");
}
}
22
Mahmoud Rafeek Alfarra