0% found this document useful (0 votes)
2 views

Lab_Sheet_5_Exception Handling

The document provides examples of exception handling in C# programming, specifically for dividing numbers and validating university names. It includes code snippets that demonstrate how to raise and catch exceptions for invalid inputs, such as division by zero and incorrect university names. Additionally, it outlines two assessments related to raising exceptions for age eligibility for voting and academic promotion criteria based on CGPA.

Uploaded by

Chinmayi D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab_Sheet_5_Exception Handling

The document provides examples of exception handling in C# programming, specifically for dividing numbers and validating university names. It includes code snippets that demonstrate how to raise and catch exceptions for invalid inputs, such as division by zero and incorrect university names. Additionally, it outlines two assessments related to raising exceptions for age eligibility for voting and academic promotion criteria based on CGPA.

Uploaded by

Chinmayi D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LAB SHEET 5

Exception Handling

1. Developer is writing code for dividing two numbers. If the user enters 0 in the second
number, an exception should be raised. Handle the exception with the appropriate
code.
namespace ConsoleApp10
{
internal class Program
{
static void Main(string[] args)
{
int a, b;

Console.WriteLine("Enter first number");


a= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second number");
b= Convert.ToInt32(Console.ReadLine());
try
{
int c = a/b;
Console.WriteLine("Answer is " + c);
}
catch(Exception e)
{
Console.WriteLine("Second Number cannot be zero");
//Console.WriteLine(e.ToString());
}
Console.ReadKey();
}

}
}
2. Admin executive of the university is entering the university name for the students. If
he enters the name wrongly, exception should be raised.
namespace ExceptionHandlingDemo
{
internal class Collegename
{
static void Main()
{
string s;
Console.WriteLine("Enter your university name");
try
{
s = Console.ReadLine();
if (s.ToUpper() == "PRESIDENCY")
{
Console.WriteLine("Welcome");
}
else
{
throw new Exception("You have entered wrong input");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}

Assessment 5a:
Raise the exception not eligible for voting if the age is less than 18.

Assessment 5b:
A University has decided promotion criteria for students. According to criteria a student
cannot be promoted to next academic year if he has less than 4.5 CGPA. A developer is
trying to implement this situation using exception handling by reading the necessary
details of all 6 subjects
Hint: CGPA = (Percentage / 9.5)

You might also like