Tutorial www.btechsmartclass.com Exceptions Handling.

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
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.
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 and Assertions Chapter 15 – CSCI 1302.
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. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CS 116 Object Oriented Programming II Lecture 10 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Object Throwable ErrorException RuntimeException.
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Introduction to OO Program Design
CS102 – Exceptions David Davenport Latest: May 2015
Chapter 12 Exception Handling and Text IO
Testing and Exceptions
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Chapter 12 Exception Handling
Exception Handling and Reading / Writing Files
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
Web Design & Development Lecture 7
Exception Handling in Java
Managing Errors and Exceptions
Chapter 13 Exception Handling
Lecture 11 Objectives Learn what an exception is.
CMSC 202 Exceptions 2nd Lecture.
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.
Chapter 12 Exception Handling and Text IO Part 1
Exception Handling Contents
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Tutorial MutliThreading.
Exceptions 10-May-19.
Why do we need exceptions?
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Tutorial www.btechsmartclass.com Exceptions Handling

This kind of situation is known as “EXCEPTION” Introduction In any programming there will be some problems like, trying to access a resource which is not available, giving wrong input values, hardware problems like network connection etc., these kind of problems will not be known at the time of program compilation. The compiler checks whether the program is as per the syntax rules of that programming language…. These problems will came to know, when we try to execute(run) that program…. So we call them as “Runtime Errors” In such a kind of situation, What the Runtime machine does? Simply, Stops program execution and causes abnormal termination. This kind of situation is known as “EXCEPTION”

An exception handling is the concept which is used to handle exception Definition Exception is an abnormal condition Dictionary Meaning: An exception is a problem that arises during the execution of a program An exception handling is the concept which is used to handle exception

Concept In java, Exception is an object that describes the exception that occurred in a program. Whenever exception occurs in a program, it creates an object of respective exception class and sends it to a block of code which handles that situation….. So that the program execution will not be terminated abruptly.

Concept Exception handling will not corrects the runtime error, but just informs the user by providing some information about that error.

Reasons An exception can occur for many different reasons, including the following…. A user has entered invalid data A file that needs to be opened cannot be found A network connection has been lost in the middle of communications or the JVM has run out of memory Physical problem like device not working, cable related problem

Handling Exceptions To handle an exception in java we use the following keywords… 1 try 2 catch 3 finally 4 throw 5 throws

Handling Exceptions To handle an exception in java we use the following keywords… 1 try 2 catch The try keyword is used to define a block of statements which may generate exception 3 finally 4 throw 5 throws

Handling Exceptions To handle an exception in java we use the following keywords… 2 catch 1 try The catch keyword is used to define a block of statements which can handle the exception occurred in try block 3 finally Every try block must have atleast one catch block 4 throw The try block may have multiple catch blocks 5 throws

Handling Exceptions To handle an exception in java we use the following keywords… 3 finally 1 try The finally keyword is used to a block of statements which must be execute irrespective of Exception occurance. 2 catch 4 throw 5 throws

The throw keyword is used to throw an exception explicitly. Handling Exceptions To handle an exception in java we use the following keywords… 4 throw 1 try The throw keyword is used to throw an exception explicitly. 2 catch 3 finally 5 throws

throw Keyword Example import java.io.*; class ThrowDemo{ public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println(“Enter any two Integer values”); int a = input.nextInt(); int b = input.nextInt(); try{ if(b == 0) throw new Exception(“Can not divide!!!”); int result = a / b; System.out.println(“Result of a / b = ”+result); } catch(Exception e){ System.out.println(e); Example

Handling Exceptions To handle an exception in java we use the following keywords… 5 throws 1 try The throws keyword is used to list the types of Exceptions that a method might throw. 2 catch 3 finally 4 throw

throws Keyword Example class ThrowsDemo { void myMethod(int n) throws IOException,ClassNotFoundException { if(n ==1) throw new IOException(“Message 1 !!!”); else throw new ClassNotFoundException(“Message 2 !!!”); } class ThrowsDemoTest { public static void main(String args[]) { ThrowsDemo obj = new ThrowsDemo(); try{ obj.myMethod(1); catch(Exception e){ System.out.println(e); Example

In java there are TWO types of exceptions Categories In java there are TWO types of exceptions Checked Exceptions The checked exceptions are checked at compile-time Unchecked Exceptions The unchecked exceptions are checked at runtime

ClassNotFoundException Checked Exceptions Checked exceptions should handle the exception using  try - catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error. IOException SQLException DataAccessException ClassNotFoundException

Checked Exceptions Example import java.io.*; class Example { public static void main(String args[]) { FileInputStream fis = new FileInputStream("B:/myfile.txt"); /*This constructor FileInputStream(File filename) throws FileNotFoundException */ int k; while(( k = fis.read() ) != -1) { System.out.print((char)k); } fis.close(); /*The method close() closes the file input stream * It throws IOException*/

Unchecked Exceptions Unchecked Exceptions mostly arise due to programming errors like accessing method of a null object, accessing element outside an array bonding or invoking method with illegal arguments etc,. NullPointerException ArrayIndexOutOfBound IllegalArgumentException IllegalStateException

NullPointerException IndexOtOfBoundsException NumberFormatException Hierarchy In java all the Exceptions are defined as Classes to handle them. All those classes are in following hierarchy… Throwable Error Exception RunTimeException IOException SQLException NullPointerException IndexOtOfBoundsException NumberFormatException …..

Exception Hierarchy

Exception Hierarchy

Creating Exceptions To create your own exception types to handle situations just define a subclass of Exception

class MyOwnException extends Exception { public MyOwnException(String msg){ super(msg); } class EmployeeTest { static void employeeAge(int age) throws MyOwnException{ if(age < 0) throw new MyOwnException("Age can't be less than zero"); else System.out.println("Input is valid!!"); public static void main(String[] args) { try { employeeAge(-2); } catch (MyOwnException e){ e.printStackTrace(); }