/
Garbage Garbage

Garbage - PowerPoint Presentation

min-jolicoeur
min-jolicoeur . @min-jolicoeur
Follow
442 views
Uploaded On 2017-08-07

Garbage - PPT Presentation

Collection It Is A Way To Destroy The Unused Objects To do so we were using free function in C language and delete in C But in java it is performed automatically So java provides better memory management ID: 576579

block exception throw method exception block method throw exceptions catch system throws println raised code class public java arithmeticexception void main string

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Garbage" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Garbage Collection

It Is A Way To Destroy The Unused Objects.

To do so, we were using free() function in C language and delete() in C++.

But

, in java it is performed automatically. So, java provides better memory management

.

In Java also we can do the Garbage Collection explicitly with the help of

gc

() method Slide2

gc() Methodgc() method is used to call garbage collector explicitly. However gc() method does not gurantee that JVM will perform the garbage collection. It only request the JVM for garbage collection.

f

inalize

() MethodIt is called by garbage collection before collecting object. Sometimes an object need to perform some specific tasks before getting destroyed such as closing an open connection or releasing any resources hold. To handle such situation finalize() is used.SyntaxSlide3

public class TestGarbage1 {protected void finalize(){ System.out.println

("object is garbage collected"); }

public static void main(String[]

args) {TestGarbage1 obj1=new TestGarbage1();TestGarbage1 obj2=new TestGarbage1();TestGarbage1 obj3=new TestGarbage1();obj1=null;obj2=null;System.gc();}} Slide4

ExceptionException is an event that disturbs the normal flow of the program

Some of these exceptions are caused by programmer

,

others by user , and others by physical resources that have failed in some manner. We have three categories of Exceptions:Checked

exceptions:

 

A checked exception is an exception that occurs at the compile time, these are also called as

compile time

exceptions

, the

Programmer should take care of (handle) these exceptions

.

For example, if you use 

FileReader

 class in your program to read data from a

file which do not existSlide5

Unchecked exceptions:  An Unchecked exception is an exception that occurs at the time of execution,

these are also called as Runtime

Exceptions

.For example, if you have declared an array of size 5 in your program, and trying to call the 6th element of the array .Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer .For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation Slide6

Why to use Exception Handlingpublic 

class

 Testtrycatch1{  

  public static void main(String args[]){        int data=50/0;//may throw exception        System.out.println("rest of the code...");  }  } 

Output

Exception in thread main

java.lang.ArithmeticException

:/ by

zero

As displayed in the above example, rest of the code is not executed (in such case, rest of the code... statement is not printed).

There can be 100 lines of code after exception. So all the code after exception will not be executed.Slide7

Therefore there is a need for exception Handlingpublic 

class

 Testtrycatch2{  

  public static void main(String args[]){     try{        int data=50/0;     }catch(ArithmeticException e){

System.out.println

(e);}  

   

System.out.println

(

"rest of the code..."

);  

}  

}

  

Output

Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...

Now, as displayed in the above example, rest of the code is executed i.e. rest of the code... statement is printed.Slide8

There are 5 keywords used in java exception handling.trycatchfinallythrowthrowsSlide9

try blockJava try block is used to enclose the code that might throw an exception.

If any exception occurs in try block then CPU controls comes out to the try block and executes appropriate catch block.

After executing appropriate catch block, even through we use run time statement, CPU control never goes to try block to execute the rest of the statements.

Each and every try block must be immediately followed by catch block that is no intermediate statements are allowed between try and catch block.Catch block

Java catch block is used to handle the Exception. It must be used after the try block only

.

You can use multiple catch block with a single try.Slide10

FinallyA finally block can be added after a try block and its catch blocks.The finally block is executed

if the try block throws no exceptions

if the try block throws an exception which is caught by a catch block

if an exception is thrown but not caughtFinally is always executedSlide11

throw, throws

throw

throws

To throw an exception explicitly throw keyword is used Any method capable of causing exceptions must list all the exceptions possible during its execution, so that anyone calling that method gets a prior knowledge about which exceptions to handle. A method can do so by using the throws keyword Slide12

throw

throws

Throw is used within method

Throws is used with method signatureYou cannot throw multiple exception with single throwYou can declare multiple exceptions with throwsSyntax : throw new Type of exception (“ string ") ;Syntax : method_name(parameter_list) throws Types of exception Slide13

Throw public class

Test

{

static void avg() { try { throw new ArithmeticException(" have a great day "); } catch(ArithmeticException e) { System.out.println(e.getMessage());} }public static void main(String args

[])

{

avg

();

}

}Slide14

Throwspublic

class

Test {static void function1()throws ArithmeticException,ArrayIndexOutOfBoundsException { try { int a=10/0;} catch(IndexOutOfBoundsException e) { System.out.println("Hello I am called method ");} }public static void main(String args[])

{

try

{

function1();

}

catch

(

ArithmeticException

ee

)

{System.out.println("Hello I am calling method");}} }Slide15

Unknown Exceptions Whenever developer do not known what type of exception is going to be raised in the try block is known as unknown Exception

.

Handling Unknown Exceptions

Use Exception class and then use any one of theseSystem.out.println ( obj ) ; System.out.println(obj.getMessage()); obj.printStackTrace(); Slide16

1. System.out.println (

obj

) ; It is used to display the Exception message along with its type 2. System.out.println(obj.getMessage());It is used to display the Exception message3.

obj.printStackTrace

();

It is used

to display the

following things

Exception

message along with its type

Package name

Exception class

name

Method name

Line number at which Exception is raisedSlide17

Checked Exception Classes

FileNotFoundException

ClassNotFoundException

IOExceptionInterruptedExceptionSlide18

FileNotFoundException : If the given filename is not available in a specific location ( in file handling concept) then

FileNotFoundException

will be raised. 

ClassNotFoundException : This exception is occured when an application tries to load a class but no definition for the specified class name could be found.IOException : This is exception is raised whenever problem occurred while writing and reading the data in the file . This exception is occurred due to following reasonWhen try to read data which is corrupted.When try to write on file but file is read only.Slide19

InterruptedException

:

This

exception is raised whenever one thread is disturb the other thread.Slide20

Un-Checked Exception ClassesArithmeticExceptionArrayIndexOutOfBoundsException

StringIndexOutOfBoundsException

NumberFormateException

NullPointerExceptionNoSuchMethodExceptionNoSuchFieldExceptionSlide21

ArithmeticException This exception is raised because of problem in arithmetic operation

like

divide by zero

.ArrayIndexOutOfBoundsException This exception will be raised whenever given index value of an array is out of range. The index is either negative or greater than or equal to the size of the array. int a[]=new int[5]; a[10]=100; //

ArrayIndexOutOfBoundsException

Slide22

StringIndexOutOfBoundsException This exception will be raised whenever given index value of string is out of range. The index is either negative or greater than or equal to the size of the array.

String

s=

"Hello"; s.charAt(10); // Exception raisedNumberFormateException This exception will be raised whenever you trying to store any input value in the un-authorized datatype.int a; a="Hello";Slide23

NoSuchMethodException This exception will be raised whenever calling method is not existing in the program

.

NullPointerException

A NullPointerException is thrown when an application is trying to use or access an object whose reference equals to null.String s=null; System.out.println(s.length());//NullPointerException StackOverFlowException This exception throw when full the stack because the recursion method are stored in stack area.