Dt : 4/11/2022
*imp
Handling Multiple Exceptons:
=>we use the following two ways to handle Multiple Exceptions:
(i)we declare multiple catch blocks to a try-block to handle Multiple
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
}
catch(Exception1 | Exception2 | ... ob)
ipa
{
//msg
}
Ma
===================================================
faq:
sh
define try-with-resource statement?
=>try-with-resource statement is introduced by Java7 version and in
ate
which we can declare resources with try.
nk
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
statement,which means no need to use finally block.
ipa
=>In try-with-resource statement catch is optional block.
=============================================================
faq:
define Enhanced try-with-resource statement?
Ma
=>Enhanced try-with-resource statement introduced by Java9 version
sh
and in which we declare resources outside the try and resource-reference
variables with try
ate
syntax:
nk
resource1;resource2;...
try(res1-var;res2-var;...)
Ve
//statements
Ex:
Scanner s = new Scanner(System.in);
try(s;)
//statements
==============================================================
i
thi
Note:
=>The classes which are declared in "try-with-resource statement" must
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
public class DemoException5 {
public static String str = null;
Ve
public static void main(String[] args) {
int len = str.length();//NullPointerException is raised
System.out.println("str:"+str.toString());
System.out.println("len of str:"+len);
}
}
o/p:
Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "String.length()" because "maccess.DemoException5.str"
is null at maccess.DemoException5.main(DemoException5.java:5)
i
thi
=============================================================
Dt : 5/11/2022
ipa
Assignment-1:
Convert BankTransaction application with Anonymous classes into
Exception handling process.
Ma
Balance.java
sh
package test;
public class Balance {
public double bal=2000;
ate
public double getBalance() {
return bal;
}
}
nk
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
public DemoException6(String msg)
sh
{
ate
super(msg);
}
nk
public static void main(String[] args)
{
Ve
Scanner s = new Scanner(System.in);
try(s;)//Java9
int count=0;
xyz:
while(true)
{
try
System.out.println("Enter the pinNo:");
int pinNo = s.nextInt();
CheckPinNo cpn = new CheckPinNo();
i
thi
boolean k = cpn.verify(pinNo);
if(!k)//Exception Condition
ipa
{
DemoException6 de = new DemoException6("Invalid pinNo");
}
throw de;
Ma
System.out.println("====Choice====");
sh
System.out.println("1.WithDraw\n2.Deposit");
System.out.println("Enter the choice:");
ate
switch(s.nextInt())
{
nk
case 1:
System.out.println("Enter the amt:");
Ve
int a1 = s.nextInt();
if(!(a1>0 && a1%100==0))//Exception Condition
DemoException6 de = new DemoException6("Invalid
amt");
throw de;
Transaction wd2 = new Transaction()
public void process(int amt)throws Exception
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:
System.out.println("Enter the amt:");
i
thi
int a2 = s.nextInt();
if(!(a2>0 && a2%100==0))//Exception Condition
ipa
{
DemoException6 de = new DemoException6("Invalid
amt"); Ma
throw de;
Transaction dp = new Transaction()
sh
{
ate
public void process(int amt)
{
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
"java.lang.Exception" directly because the class name is not available.
===============================================================
sh
Assignment-2:
Convert BankTransaction application with LambdaExpressions into
ate
Exception handling process.
nk
Balance.java
package test;
Ve
public class Balance {
public double bal=2000;
public double getBalance() {
return bal;
}
}
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
public class DemoException7 extends Exception
{
nk
public DemoException7(String msg)
{
Ve
super(msg);
public static void main(String[] args)
Scanner s = new Scanner(System.in);
try(s;)//Java9
int count=0;
xyz:
while(true)
i
thi
try
ipa
System.out.println("Enter the pinNo:");
int pinNo = s.nextInt();
Ma
CheckPinNo cpn = new CheckPinNo();
boolean k = cpn.verify(pinNo);
if(!k)//Exception Condition
sh
{
DemoException7 de = new DemoException7("Invalid pinNo");
ate
throw de;
}
nk
System.out.println("====Choice====");
System.out.println("1.WithDraw\n2.Deposit");
Ve
System.out.println("Enter the choice:");
switch(s.nextInt())
case 1:
System.out.println("Enter the amt:");
int a1 = s.nextInt();
if(!(a1>0 && a1%100==0))//Exception Condition
DemoException7 de = new DemoException7("Invalid
amt");
throw de;
i
}
thi
Transaction wd2 = (int amt)->
ipa
try
{ Ma
if(amt>Transaction.b.bal)//Exception condition
Exception wd = new Exception("Insufficient
sh
fund");
ate
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:
System.out.println("Enter the amt:");
ipa
int a2 = s.nextInt();
if(!(a2>0 && a2%100==0))//Exception Condition
{
Ma
DemoException7 de = new DemoException7("Invalid
amt");
throw de;
sh
}
ate
Transaction dp = (int amt)->
{
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
{
System.out.println("Enter only Integer value...");
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:
=>In the process of handling exception in LambdaExpressions,we handle
Ma
"java.lang.Exception" directly because the Class and method_name is not
available.
==================================================================
sh
faq:
define Encapsulation process?
ate
=>The process of binding all the programming components into a single
unit class is known as Encapsulation process.
nk
Comparision Diagram:
Ve
i
thi
ipa
================================================================
Ma
faq:
wt is the diff b/w
sh
(i)function
(ii)member function
ate
(iii)method
nk
(i)function:
Ve
=>The part of program which is executed out of main-program in c-lang
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
wt is the diff b/w classes in c++ and Classes in Java?
=>classes in c++ will hold Variables and functions,but cannot hold main().
Ve
=>classes in Java will Variables,methods and main()
====================================================================
faq:
define Annotation?
=>The tag based information which is added to the programming component
like Interface,class,method and variable is known as Annotation.
=>we use "@" symbol to represent annotations
=>These Annotations will give information to compiler at compilation
stage.
=>The following are two important annotations in CoreJava:
(i)@SuppressWarnings
i
thi
(ii)@Override
ipa
(i)@SuppressWarnings:
=>@SuppressWarnings annotation will provide information to compiler,to
close the raised Warnings.
Ma
(ii)@Override:
sh
=>@Override annotation will provide information to compiler, to check
the method is Overriding method or not
ate
======================================================================
nk
Ve