Professional Documents
Culture Documents
Exception Handling: Reg - No: URK20CS1116
Exception Handling: Reg - No: URK20CS1116
Exception Handling: Reg - No: URK20CS1116
Reg.No: URK20CS1116
Ex.No: 7
Exception Handling
Date: 07/09/2021
1 Aim:
To read two integers x and y as input and to compute x/y. If x and y are not 32-bit
signed integers or if y zero, exception will occur and you have to report it.
Description:
Step 1: Start the program.
Step 2: Use try and read a and b integer.
Step 3: Compute c = a/b.
Step 4: Display c.
Step 5: Use catch to display error in case any error occurs.
Step 6: Stop the program.
Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
System.out.println(e);
}
}
}
Sample Output:
2 Aim:
To complete the function power in class MyCalculator and return the appropriate
result after the power operation or an appropriate exception.
Description:
Step 1: Start the program.
Step 2: Create while loop and use scan.hasNextInt().
Step 3: Use try and read n and p integer and display power(n,p).
Step 4: Use catch to display error in case any error occurs.
Step 5: Create power function to get integer n and p throws Exception.
Step 6: Declare pow is equal to 1.
Step 7: If n and p is equal to 0 then use throw new Exception to print "n and p
should not be zero".
Step 8: Else if n or p is less than 0 then use throw new Exception to print "n and p
should not be negative".
Step 9: Else create while loop (p > 0) then compute pow = pow * n and decrement p
and return pow.
Step 10: Stop the program.
Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
}
}
Sample Output:
4
3 Aim:
To read a string S , and print its integer value; if S cannot be converted to an integer
then print Bad String.
Description:
Step 1: Start the program.
Step 2: Declare String S and read input S from user.
Step 3: Use try and Convert String S to integer I.
Step 4: Display i.
Step 5: Use catch to display “Bad String” in case NumberFormatException error
occurs.
Step 6: Stop the program.
Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
try{
int i = Integer.parseInt(S);
System.out.println(i);
}catch(NumberFormatException nfe){
System.out.println("Bad String");
5
}
}
}
Sample Output:
4 Aim:
To create an array of characters which will be initialized during run time with
vowels. If user enters any consonant, your code should generate a user-defined
checked exception, InvalidVowelException. The description or message of
InvalidVowelException is "character is consonant". Handle the exception by using
try, catch, finally, throw and throws.
Description:
Step 1: Start the program.
Step 2: Create class InvalidVowelException and extends Exception.
Step 3: Use public String toString function to return "character is consonant".
Step 4: Create class cv and declare char ch.
Step 4: Use constructor to initialize char ch.
Step 5: Create check function throws InvalidVowelException.
Step 6: If char ch is equal to vowels then add char ch to character array.
Step 7: Else use throw new InvalidVowelException.
Step 8: In main class display "Enter number of character: ".
Step 9: Read input integer n from user.
Step 10: declare character array a.
Step 11: Use try and create for loop to add characters on array a and use check
function to check character is vowel or not.
Step 12: Use catch to display error in case any error occurs.
Step 13: Use finally to display "Program is stopped".
Step 14: Stop the program.
Code:
import java.util.Scanner;
6
class cv
{
char ch;
cv(char b)
{
ch = b;
}
}
else
{
throw new InvalidVowelException();
}
}
public class vowel {
b.check();
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("Program is stopped");
}
Sample Output:
5 Aim:
To write a menu driven program in Java to automate the Banking operations by
demonstrating the concept of multi-level inheritance. Create custom exceptions to
deal with the following situations.
a. Account is created with Initial Balance < 500.
b. Withdraw operation when balance amount < withdraw amount.
Description:
Step 1: Start the program.
Step 2: Create class Account and declare String acc_name, int acc_no, int balance.
Step 3: Use constructor to initialize String acc_name, int acc_no, int balance.
Step 4: Create account function throws customerror to get account details.
Step 5: Check if initial balance is less than 500 then use throw new customerror to
display "Account is not created because of insufficient initial balance !".
Step 6: Else display "Account is created".
Step 7: Create get_aname and get_anum function to return Account name and
number.
8
Step 8: Create class customerror extends Exception and declare String msg and use
constructor to initialize msg.
Step 9: Create String toString function return msg.
Step 10: Create class Bank extends Account and use constructor to initialize super
variables.
Step 11: Create deposit function to deposit money.
Step 12: Create withdraw function throws customerror to withdraw money.
Step 13: Create viewBalance function to display balance.
Step 14: Create Add function to add account name and account number.
Step 15: In main class initialize object and create two object and give two account
details.
Step 16: Display 4 options 1) Create Account, 2) Deposit, 3) Withdraw, 4) Balance
Enquiry.
Step 17: Display "Enter your choice: " and read input from user.
Step 18: Use switch case and if user entered 1 then use account function to create
account and in case any error then display error using catch and use finally to
display "Error is checked".
Step 19: Else if user entered 2 then use deposit function to deposit money.
Step 20: Else if user entered 3 then use withdraw function throws customerror to
withdraw money and in case any error then display error using catch and use finally
to display "Error is checked".
Step 21: Else if user entered 4 then use viewBalance function to display balance.
Step 22: Else user entered any other number then display “Please enter correct
choice!”.
Step 23: Stop the program.
Code:
import java.util.Scanner;
class Account
{
Scanner sc = new Scanner(System.in);
public String acc_name;
public int acc_no;
public int balance;
Account(String acc_name, int acc_no, int balance)
{
this.acc_name = acc_name;
this.acc_no = acc_no;
this.balance = balance;
}
public void deposit() {
}
public void withdraw() throws customerror {
}
public void viewBalance() {
9
}
public void Display() {
}
public void Add() {
}
void account() throws customerror
{
System.out.print("Enter Account name : ");
this.acc_name = sc.nextLine();
System.out.print("Enter Account number : ");
this.acc_no = sc.nextInt();
System.out.print("Enter balance : ");
this.balance = sc.nextInt();
if (this.balance < 500) {
this.acc_name = null;
this.acc_no = 0;
this.balance = 0;
throw new customerror("Account is not created becuase of insufficient initial
balance !");
}
else {
System.out.print("Account is created");
}
}
String get_aname(){
return this.acc_name;
}
int get_anum(){
return this.acc_no;
}
}
class customerror extends Exception
{
String msg;
customerror(String x)
{
msg=x;
}
}
}
if(p == 0) {
System.out.println("Account is not available on this bank please enter correct
account's details");
}
break;
case 3:
Account c = new Bank(null, 0, 0);
c.Add();
for (int j = 0; j<m;j++)
{
if(c.get_aname().equals(a[j].get_aname()))
{
if (c.get_anum() == a[j].get_anum())
{
try {
p++;
a[j].withdraw();
}
catch(customerror e) {
System.out.println(e);
}
finally {
System.out.println("Error is checked");
}
break;
}
}
}
if(p == 0) {
System.out.println("Account is not available on this bank please enter correct
account's details");
}
break;
case 4:
Account d = new Bank(null, 0, 0);
d.Add();
for (int j = 0; j<m;j++)
{
if(d.get_aname().equals(a[j].get_aname()))
{
if (d.get_anum() == a[j].get_anum())
{
a[j].viewBalance();
p++;
13
break;
}
}
}
if(p == 0) {
System.out.println("Account is not available on this bank please enter correct
account's details");
}
break;
default:
System.out.println("Please enter correct choice !");
break;
}
}
Sample Output:
Video:
https://drive.google.com/file/d/1R3DqbOn4r4_tQ5w3-7b9oAjLkLmOSalC/view?
usp=sharing
Result:
The above program has been executed for sample input values and output is
verified.