Class Running Notes 4th and 5th Nov
Class Running Notes 4th and 5th Nov
*imp
i
thi
Exceptions
ipa
syntax:
try
//Exception1
Ma
//Exception2
sh
}
catch(Exception1 ob)
ate
//msg
nk
catch(Exception2 ob)
Ve
//msg
(ii)From Java7 version onwards we can use single catch block to hold
Multiple exceptions.
try
//Exception1
//Exception2
i
thi
}
ipa
{
//msg
}
Ma
===================================================
faq:
sh
define try-with-resource statement?
syntax:
try(resource1;resource2;...)
Ve
//statements
Ex:
try(Scanner s = new Scanner(System.in);)
//statements
Advantage:
i
thi
=>The resources will be closed automatically in try-with-resource
ipa
=>In try-with-resource statement catch is optional block.
=============================================================
faq:
syntax:
nk
resource1;resource2;...
try(res1-var;res2-var;...)
Ve
//statements
Ex:
Scanner s = new Scanner(System.in);
try(s;)
//statements
==============================================================
i
thi
Note:
ipa
be implementations of "java.lang.AutoCloseable" interface.
============================================================
faq:
define "java.lang.NullPointerException"?
Ma
=>"java.lang.NullPointerException" is raised when we use NonPrimitive
sh
datatype variable assigned with null value.
ate
Ex:
package maccess;
nk
System.out.println("str:"+str.toString());
System.out.println("len of str:"+len);
}
}
o/p:
is null at maccess.DemoException5.main(DemoException5.java:5)
i
thi
=============================================================
Dt : 5/11/2022
ipa
Assignment-1:
CheckPinNo.java
Ve
package test;
public class CheckPinNo {
public boolean verify(int pinNo) {
return switch(pinNo) {
case 1111 : yield true;
case 2222 : yield true;
case 3333 : yield true;
default : yield false;
};
}
}
Transaction.java
package test;
public interface Transaction {
public static final Balance b = new Balance();
public abstract void process(int amt)throws Exception;
}
i
DemoException6.java(MainClass)
thi
package maccess;
import java.util.*;
ipa
import test.*;
@SuppressWarnings("serial") Ma
public class DemoException6 extends Exception
super(msg);
}
nk
{
Ve
try(s;)//Java9
int count=0;
xyz:
while(true)
{
try
i
thi
boolean k = cpn.verify(pinNo);
if(!k)//Exception Condition
ipa
{
}
throw de;
Ma
System.out.println("====Choice====");
sh
System.out.println("1.WithDraw\n2.Deposit");
switch(s.nextInt())
{
nk
case 1:
int a1 = s.nextInt();
i
thi
try
ipa
if(amt>b.bal)//Exception condition
fund");
Ma Exception wd = new Exception("Insufficient
throw wd;
sh
}
ate
System.out.println("Amt withDrawn:"+amt);
b.bal=b.bal-amt;
nk
System.out.println("Balance
amt:"+b.getBalance());
Ve
System.out.println("Transaction Completed...");
}//end of try
catch(Exception wd)
throw wd;//re-throwing
}
}
};
wd2.process(a1);//method Call
break xyz;
case 2:
i
thi
int a2 = s.nextInt();
ipa
{
{
nk
System.out.println("Amt deposited:"+amt);
b.bal=b.bal+amt;
Ve
System.out.println("Balance
amt:"+b.getBalance());
System.out.println("Transaction completed...");
};
dp.process(a2);
break xyz;
default:
System.out.println("Invalid Choice...");
break xyz;
}//end of switch
}//end of try
i
thi
catch(InputMismatchException ime)
ipa
System.out.println("Enter only Integer value...");
break xyz;
catch(Exception de)
Ma
{
sh
System.out.println(de.getMessage());
if(de.getMessage().equals("Invalid pinNo"))
ate
count++;
nk
if(count==3)//Nested Simple if
{
Ve
System.out.println("Transaction blocked...");
break xyz;
}//end of if
else
{
break xyz;
}//end of loop
i
thi
}//end of try-with-resource
ipa
}
Note:
Ma
=>In the process of handling exception in Anonymous classes,we handle
===============================================================
sh
Assignment-2:
Balance.java
package test;
Ve
CheckPinNo.java
package test;
public class CheckPinNo {
public boolean verify(int pinNo) {
return switch(pinNo) {
case 1111 : yield true;
case 2222 : yield true;
case 3333 : yield true;
default : yield false;
};
}
}
i
thi
Transaction.java
package test;
public interface Transaction {
ipa
public static final Balance b = new Balance();
public abstract void process(int amt)throws Exception;
}
DemoException7.java(MainClass)
Ma
package maccess;
import java.util.*;
sh
import test.*;
ate
{
nk
{
Ve
super(msg);
int count=0;
xyz:
while(true)
i
thi
try
ipa
System.out.println("Enter the pinNo:");
boolean k = cpn.verify(pinNo);
if(!k)//Exception Condition
sh
{
throw de;
}
nk
System.out.println("====Choice====");
System.out.println("1.WithDraw\n2.Deposit");
Ve
switch(s.nextInt())
case 1:
throw de;
i
}
thi
Transaction wd2 = (int amt)->
ipa
try
{ Ma
if(amt>Transaction.b.bal)//Exception condition
throw wd;
}
nk
System.out.println("Amt withDrawn:"+amt);
Ve
Transaction.b.bal=Transaction.b.bal-amt;
System.out.println("Balance
amt:"+Transaction.b.getBalance());
System.out.println("Transaction Completed...");
}//end of try
catch(Exception wd)
{
throw wd;//re-throwing
};
wd2.process(a1);//method Call
break xyz;
i
thi
case 2:
ipa
int a2 = s.nextInt();
{
Ma
DemoException7 de = new DemoException7("Invalid
amt");
throw de;
sh
}
ate
{
nk
System.out.println("Amt deposited:"+amt);
Transaction.b.bal=Transaction.b.bal+amt;
Ve
System.out.println("Balance
amt:"+Transaction.b.getBalance());
System.out.println("Transaction completed...");
};
dp.process(a2);
break xyz;
default:
System.out.println("Invalid Choice...");
break xyz;
}//end of switch
}//end of try
catch(InputMismatchException ime)
i
thi
{
ipa
break xyz;
catch(Exception de)
{
Ma
System.out.println(de.getMessage());
sh
if(de.getMessage().equals("Invalid pinNo"))
{
ate
count++;
if(count==3)//Nested Simple if
nk
System.out.println("Transaction blocked...");
Ve
break xyz;
}//end of if
else
{
break xyz;
}//end of loop
}//end of try-with-resource
i
thi
}
ipa
Note:
available.
==================================================================
sh
faq:
Comparision Diagram:
Ve
i
thi
ipa
================================================================
Ma
faq:
(ii)member function
ate
(iii)method
nk
(i)function:
Ve
is known as function.
(ii)member function:
=>The functions which are declared part of classes in c++ lang are known
as member functions.
Note:
=>member functions can be declared inside the class and outside class.
(iii)method:
=>The functions which are declared only inside the class in Java-lang
i
thi
are known as methods.
ipa
Ma
sh
ate
=================================================================
faq:
nk
=>classes in c++ will hold Variables and functions,but cannot hold main().
Ve
====================================================================
faq:
define Annotation?
stage.
(i)@SuppressWarnings
i
thi
(ii)@Override
ipa
(i)@SuppressWarnings:
======================================================================
nk
Ve