You are on page 1of 21

Dt : 20/7/2022

Diagram:

==========================================================

faq:

define getMessage() method?

=>getMessage() method is from 'Throwable' class and which is

used to display the exception_message.

Method Signature:
public java.lang.String getMessage();

syntax:

String msg = obj.getMessage();

faq:

define printStackTrace() method?

=>printStackTrace() method is also from 'Throwable' class and

which is used to display the details of exception like

package_name,Class_name,method_name and line_no.

Method Signature:

public void printStackTrace();

syntax:

obj.printStackTrace();

======================================================

*imp

Types of Checked Exceptions:

=>These Checked Exceptions are categorized into two types:

(i)Built-in Checked Exceptions

(ii)User defined Checked Exceptions

(i)Built-in Checked Exceptions:

=>The Checked Exceptions which are available from JavaLib are

known as Built-in Checked Exceptions or Pre-defined Checked


Exceptions.

Ex:

java.lang.InterruptedException

java.lang.ClassNotFoundException

java.io.IOException

...

faq:

define sleep() method?

=>sleep() method is from java.lang.Thread class and which is

used to stop the execution process temporarly on some time.

Method Signature:

public static native void sleep(long)

throws java.lang.InterruptedException;

syntax:

Thread.sleep(time);

Note:

(i)'throws' keyword specify to ignore the exception in current

method and raise at method_call

(ii)when the exception is raised at method_call,then the

compiler will identify the exception and known as Checked


exception or Compile time exception.

Ex : DemoException4.java

package maccess;

public class DemoException4 {

public static void main(String[] args) {

for(int i=1;i<=10;i++) {

System.out.println("Value:"+i);

try {

Thread.sleep(5000);//method_call

}catch(InterruptedException ie) {ie.printStackTrace();}

}//end of loop

o/p:

Value:1

Value:2

Value:3

Value:4

Value:5

Value:6

Value:7
Value:8

Value:9

Value:10

=====================================================

Dt : 21/7/2022

*imp

(ii)User defined Checked Exceptions:

=>The checked exceptions which are defined and raised by the

programmer are known as User defined Checked Exception or Custom

Checked Exceptions.

=>we use the following steps to define and raise the Checked

Exceptions:

step-1 : The method signature is added with 'throws' keyword,

which specify to ignore the exception in current method

and raise at method_call

step-2 : declare 'throw' keyword in 'catch' block and perform

re-throwing process.

Ex : (Solution fot Student-details-result)

StudentDetails.java

package test;
public class StudentDetails {
public String rollNo,name,branch,result;
public int totMarks;
public float per;
public String toString() {
return rollNo+"\t"+name+"\t"+
branch+"\t"+totMarks+"\t"
+"\t"+per+"\t"+result;
}
}

CheckBranch.java

package test;
@SuppressWarnings("serial")
public class CheckBranch extends Exception
{
public CheckBranch() {}
public CheckBranch(String msg)
{
super(msg);
}
public void verify(String br) throws CheckBranch
{
try
{
switch(br) {
case "CSE":
break;
case "ECE":
break;
case "EEE":
break;
default: //raise the exception
CheckBranch cb =
new CheckBranch("Invalid branch...");
throw cb;
}//end of switch
}//end of try
catch(CheckBranch cb)
{
throw cb;//re-throwing process
}
}//end of method
}
GenerateBranch.java

package test;
public class GenerateBranch{
public String generate(String brCode) {
return switch(brCode) {
case "05":
yield "CSE";
case "04":
yield "ECE";
case "02":
yield "EEE";
default:
yield "null";
};
}
}

StudentResult.java

package test;
public class StudentResult {
public String generateResult(float per,boolean p)
{
if(p)
{
return "Fail";
}
else if(per>=70 && per<=100)
{
return "Destinction";
}
else if(per>=60 && per<70)
{
return "FirstClass";
}
else if(per>=50 && per<60)
{
return "SecondClass";
}
else if(per>=35 && per<50)
{
return "ThirdClass";
}
else
{
return "Fail";
}
}
}

DemoException5.java(MainClass)

package maccess;

import test.*;

import java.util.*;

@SuppressWarnings("serial")

public class DemoException5 extends Exception

public DemoException5(String msg)

super(msg);

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

try(s;)//Java

ArrayList<StudentDetails> al =

new ArrayList<StudentDetails>();

try

System.out.println("Enter the number of Students:");

int n = Integer.parseInt(s.nextLine());
for(int i=1;i<=n;i++)

try

StudentDetails sd = new StudentDetails();

System.out.println("Enter the rollNo-"+i);

sd.rollNo=s.nextLine();

if(sd.rollNo.length()!=10)//Exception

DemoException5 de = new DemoException5

("Invalid rollNo..");

throw de;

}//end of if

System.out.println("Enter the StudentName-"+i);

sd.name=s.nextLine();

System.out.println("Enter the branch-"+i);

sd.branch=s.nextLine().toUpperCase();

CheckBranch cb = new CheckBranch();

cb.verify(sd.branch);

GenerateBranch gb = new GenerateBranch();

String br =

gb.generate(sd.rollNo.substring(6,8));

if(br==null)

{
DemoException5 ob = new DemoException5

("Invalid branchCode in rollNo..");

throw ob;

if(!br.equals(sd.branch))

DemoException5 ob = new DemoException5

("RollNo not belongs to branch..");

throw ob;

System.out.println("Six SubMarks for Student-"+i);

int j=1,totM=0;

boolean p=false;

while(j<=6)

try

System.out.println("Marks of Sub-"+j);

int sub = Integer.parseInt(s.nextLine());

if(sub<0 || sub>100)

DemoException5 ob = new DemoException5

("Invalid marks...");

throw ob;
}

if(sub>=0 && sub<=34)

p=true;

totM=totM+sub;

j++;

}//end of switch

catch(NumberFormatException nfe)

System.out.println("Only Integer value..");

catch(DemoException5 ob)

System.out.println(ob.getMessage());

}//end of while

sd.totMarks=totM;

sd.per=(float)sd.totMarks/6;

sd.result =

new StudentResult().generateResult(sd.per,p);

al.add(sd);//adding to ArrayList object

}//end of try

catch(DemoException5 | CheckBranch ob)


{

System.out.println(ob.getMessage());

i--;

}//end of loop

System.out.println("====StudentDetails===");

al.forEach((k)->

System.out.println(k.toString());

});

}//end of try

catch(NumberFormatException ob)

System.out.println("Enter only integer value..");

}//end of Outer Enhanced try-with-resource

}
===========================================================

faq:

define Exception re-throwing process?

=>The process of declaring 'throw' keyword in catch block and

throwing the exception is known as Exception re-throwing process.

Note:

=>In Exception re-throwing process the object reference is

handled by the catch block of try block where method call is

declared.

faq:
define Exception propagation?

=>In Exception re-throwing process the exception is moved from

one method to another method is known as Exception Propagation.

faq:

wt is the diff b/w

(i)throw

(ii)throws

=>'throw' keyword is used to throw the object reference onto catch

block,in the process of handling exception.

=>'throws' keyword added with method signature to ignore the

exception in current running method and raise at method_call.

==========================================================

faq:

define Annotation?

=>The tag based information which is added to the programming

components(Variable,method,class and Interface) is known as

Annotation.

=>These annotations are represented using '@' symbol.

=>The following are some important annotation from CoreJava:

(i) @Override

(ii) @SuppressWarnings
(i) @Override:

=>This '@Override' will specify the compiler to check the

method is Overriding method or not.

(ii) @SuppressWarnings:

=>This '@SuppressWarnings' will specify the compiler to close

the raised warnings.

============================================================

Dt : 22/7/2022

faq:

define try-with-resource?

=>The process of declaring the resources with try is known as

try-with-resource introduced by Java7 version.

syntax:

try(resource1;resource2;...)

//statements

Ex:

try(Scanner s = new Scanner(System.in);)

//statements
}

=>In try-with-resource statement the resources are opened and

closed automatically,which means 'finally' block not needed.

=>'catch' block is not manditory for try-with-resource statement

==========================================================

faq:

define 'Enhanced try-with-resource statement'?

=>In 'Enhanced try-with-resource statement' the resources are

declared outside the try and resource ref_variables are declared

with try introduced by Java9 version.

syntax:

resource1;resource2;...

try(res1_var;res2_var;...)

//statements

Ex:

Scanner s = new Scanner(System.in);

try(s;)

//statements
}

================================================

faq:

define java.lang.NullPointerException?

=>when we perform some operation using NonPrimitive datatype

variable assigned with 'null' value will raise

'NullPointerException'.

(NullPointerException is raised when we use NonPrimitive datatype

variable assigned with null value)

======================================================

*imp

MultiThreading process:

define application?

=>The set-of-programs collected together to perform defined

action is known as application.

define process?

=>The application under execution is a process.

(According to JavaLang)

define Task?

=>The part of process is known as task.


Note:

=>According to JavaApp,each program in an application is a Task

define Mult-Tasking?

=>Executing Multiple tasks Simultaneously is known as

Multi-Tasking.

(Simultaneously means at-a-time but not parallel)

define Thread based Multi-Tasking?

=>Executing multiple tasks simultaneously from the same process

is known as Thread based Multi-Tasking.

define Thread?

=>The part of task is known as thread.

Note:

=>Thread is also known as LightWeight and Background process

LightWeight - means consumes less execution time

BackGround process - means there is no separate identification

=>Thread is also known as ChildProcess.

define Multi-Threading process?

=>The process of executing multiple threads simultaneously is

known as Multi-Threading process.


Diagram:

===================================================

*imp

Creating and executing thread using 'java.lang.Runnable'

interface:

Model-1 : Creating thread using Interface and Implementation class

name

ProjectName : Interface_App5

packages,
test : Display1,Display2

maccess : DemoInterface5(MainClass)

Model-2 : Creating thread using Interface and Anonymous

InnerClass as implementation class

ProjectName : Anonymous_InnerClass_App4_Thread

packages,

maccess : DemoAnonymous4.java(MainClass)

Model-3 : Creating thread using LambdaExpressions

ProjectName : App3_LambdaExpression_Thread

packages,

maccess : DemoLambdaExpression3.java(MainClass)

Model-4 : Creating thread using Method references

ProjectName : App_MethodReferences_Thread

packages,

test : Display1,Display2
maccess : DemoMethodReference2.java(MainClass)

============================================================

*imp

define Thread synchronization process?

=>The process of ordering the threads for execution is known as

Thread Synchronization process.

=>This Thread synchronization process can be done in two ways:

1.Mutual Exclusion process

2.Thread Communication process

You might also like