Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 gc() Method gc() 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. finalize () Method It 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. Syntax

3 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(); }

4 Exception Exception 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 exist

5 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

6 Why to use Exception Handling public 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.

7 Therefore there is a need for exception Handling public 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.

8 There are 5 keywords used in java exception handling.  try  catch  finally  throw  throws

9 try block  Java 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.

10 Finally A 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 caught Finally is always executed

11 throw, throws throwthrows 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

12 throwthrows Throw is used within methodThrows is used with method signature You cannot throw multiple exception with single throw You can declare multiple exceptions with throws Syntax : throw new Type of exception (“ string ") ; Syntax : method_name(parameter_list) throws Types of exception

13 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(); }

14 Throws public 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"); }

15 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 these 1.System.out.println ( obj ) ; 2. System.out.println(obj.getMessage()); 3. obj.printStackTrace();

16 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 message 3. 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 raised

17 Checked Exception Classes  FileNotFoundException  ClassNotFoundException  IOException  InterruptedException

18 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 reason When try to read data which is corrupted. When try to write on file but file is read only.

19 InterruptedException : This exception is raised whenever one thread is disturb the other thread.

20 Un-Checked Exception Classes  ArithmeticException  ArrayIndexOutOfBoundsException  StringIndexOutOfBoundsException  NumberFormateException  NullPointerException  NoSuchMethodException  NoSuchFieldException

21 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

22 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 raised NumberFormateException This exception will be raised whenever you trying to store any input value in the un-authorized datatype. int a; a="Hello";

23 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.


Download ppt "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."

Similar presentations


Ads by Google