-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandling.cs
More file actions
37 lines (33 loc) · 1 KB
/
Copy pathExceptionHandling.cs
File metadata and controls
37 lines (33 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
class ExceptionHandling
{
static void Main()
{
double[] arr1 = { 10, 5, 2, 8, 4 };
double[] arr2 = { 2, 1, 0, 4, 2 };
try
{
if(arr1.Length != arr2.Length)
{
throw new IndexOutOfRangeException("Arrays must be of the same length.");
}
for (int i = 0; i < arr1.Length; i++)
{
double result = arr1[i] / arr2[i];
Console.WriteLine("Result of {0} / {1} = {2}", arr1[i], arr2[i], result);
}
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Error: Division by zero is not allowed. " + ex.Message);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Error: Array index is out of bounds. " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An unexpected error occurred: " + ex.Message);
}
}
}