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.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Yoshi
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Slides Credit Umair Javed LUMS Web Application Development.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error= ; double x0.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
COMPSCI 230 S Programming Techniques
Creating and Modifying Text part 2
Multithreading in Java
TRY CATCH BLOCK By Kosala Rajapaksha.
Web Design & Development Lecture 7
Exception Handling in Java
Managing Errors and Exceptions
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Tutorial Exceptions Handling.
Chapter 12 Exception Handling and Text IO Part 1
Tutorial MutliThreading.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Exception Handling.
Presentation transcript:

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

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

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

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

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

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.

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.

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

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.

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

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

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

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

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

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

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

Checked Exception Classes  FileNotFoundException  ClassNotFoundException  IOException  InterruptedException

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.

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

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

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

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";

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.