Java Basics Exception Handling.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Yoshi
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
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.
For use of Cleveland State's IST410 Students only 1 Exception.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
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.
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.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
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.
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.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
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.
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.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Introduction to OO Program Design
COMPSCI 230 S Programming Techniques
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Handling Exceptions.
Advanced Programming Behnam Hatami Fall 2017.
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.
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Abdulmotaleb El Saddik University of Ottawa
Exception Handling and Reading / Writing Files
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Exception Handling in Java
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Java Exceptions Dan Fleck CS211.
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Tutorial Exceptions Handling.
Chapter 12 Exception Handling and Text IO Part 1
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.
Exception Handling and Event Handling
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Java Basics Exception Handling

Topics To Be Covered Objective Exception Handling Summary

Objectives Exception Handling At the end of this course, you will be able to understand: Exception Handling

Exception Handling

Exception Handling A built in mechanism for trapping & handling runtime errors Usually deals with abnormal events or code execution which prevents the program from continuing, like: Array out of bounds accesses Divide by Zero Null pointers & so on... www.prolearninghub.com

Exception Handling An exception is an event, which occur during the execution of a program that disrupt the normal flow of a program’s instruction. Exception handling in java When error occur the program through an exception, when it through the system attempts to handle it. There are verity of methods to handle this. The list of methods is known as call stack. www.prolearninghub.com

Exception Handling What is Exception? An Exception is a runtime error To represent all types of exceptions there Is an Exception class in Java A variety of subclasses allows handling different kinds of errors & abnormal events Basic concept: Whenever an abnormal event occurs. Java throws an Exception It means Java creates an object of subclass of the Exception class Whenever an Exception could possibly be thrown, we must provide a mechanism for catching it in our code www.prolearninghub.com

Kind of Exception There are three kind of exceptions: Checked exceptions Errors Runtime exceptions www.prolearninghub.com

Kind of Exception Checked Exception: These are exceptional conditions that a well written program should anticipate and recover from. For example an application prompts a user from an input file name, and then open the file by passing the name to the constructor. IOException, SQLException are checked exceptions. www.prolearninghub.com

Kind of Exception Errors: These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example an application is successfully opens a file for input, but is unable to read the file because of the hardware of system malefaction . Examples: OutOfMemoryError, VirtualMachineError, AssertionError are errors. www.prolearninghub.com

Kind of Exception Runtime exceptions: These are exceptional conditions that are internal to the application, and that are application could not recover or anticipate from. Examples: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException www.prolearninghub.com

Exception Handling Catching Exception A try statement executes a block and oversees the execution of enclosed statements for exceptions A try also defines the scope for exception handlers (defined in catch clause) A try block must be accompanied by at least one catch block or one finally block Any method declared as being able to throw an Exception, can have a try / catch block to handle the exception www.prolearninghub.com

Exception Handling Catching Exception Java try block is used to enclose the code that might throw an exception. It must be used within the method. Java try block must be followed by either catch or finally block. www.prolearninghub.com

Exception Handling Catching Exception Syntax of java try-catch try{ //code that may throw exception   }catch(Exception_class_Name ref){}   Syntax of try-finally block }finally{}   www.prolearninghub.com

Exception Handling Exception Example public class ExceptionDemo1 { public static void main(String [ ] args) { int [ ] x = {1,2,3}; try { System.out.println(“x[0] = * + x[0]); System.out.println("x[5] = * + x[5]); } catch(ArraylndexOutOfBoundsException ae) { System.out.println("Index Does Not Exist"); www.prolearninghub.com

Exception Handling Exception Example 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...");   }  www.prolearninghub.com

Exception Handling Exception Hierarchy Object Throwable Error . . . RuntimeException Exception Handling Exception Hierarchy www.prolearninghub.com

Exception Handling Categories of Exception Java exceptions fall in two categories: Unchecked Not checked by the compiler at compile time Does not force the client program / method to declare each exception thrown by a method, or even handle it All exceptions are derived from RuntimeException class Checked Checked by the compiler to see if these exceptions are properly caught or specified, & if not. the code fails to compile Forces client program to deal with the scenario in which an exception may be thrown All exceptions which are not derived from RuntimeException class www.prolearninghub.com

Exception Handling Multiple Catch Blocks try { String text = "text"; A method can throw more than one possible Exceptions, or the try block could call two different methods that throw two different Exceptions try { String text = "text"; System.out.println(text.charAt(10)); int n = Integer.parseInt("abc"); } catch(IndexOutOfBoundsException e) { System.err.printIn(“Index out of bounds"); e.printStackTrace(); } catch(NumberFormatException e) { System.err.printIn("bad number"); e.printStackTrace(); } www.prolearninghub.com

Exception Handling Multiple Catch Blocks try { String text = "text"; Since all Exceptions are subclasses of the Exception class, we can generalize catch blocks to accept multiple different types of Exceptions by using a super class try { String text = "text"; System.out.println(text.charAt(10)); int n = Integer.parseInt("abc"); } catch(Exception e) { System.err.printIn(“Something bad happened"); e.printStackTrace(); } www.prolearninghub.com

Exception Handling The finally Block Sometimes, while in a try / catch block, an Exception could be thrown before some important code at the end of the try block The finally block can be used to run this code Code in finally always executes (even in case of unhandled exceptions) try { String text = "text"; System.out.println(text.charAt(10)); } catch(lndexOutOfBoundsException e) { System.err.println("Index out of bounds"); e.printStackTrace(); } finally { System.out.println("This is going to Excecute always"); } www.prolearninghub.com

Exception Handling Exception Propagation Exceptions are always propagated from the called method to the caller method, if thrown from the called method If an Exception is thrown from the main() method, it will be propagated to the Java Runtime In exception propagation, all statement executions are ignored until finding the exception handler www.prolearninghub.com

Exception Handling public class Propagate { void calculate() { int n = 25, i = 0; i = n/i; } public static void main(String[] args) { Propagate p = new Propagate(); p.calculate(); Exception propagated from calculate () to main () method Arithmetic Exception Occurred Exception in thread “main" java.lang.ArithmeticException: / by zero at Propagate.calculate(Propagate.java:4) at Propagate.main(Propagate.java:8) Exception propagated from main() function to Java www.prolearninghub.com

Summary

Summary In this session, we have covered: Exception Handling Introduction Checked & Unchecked Exceptions Using try, catch, finally Exception Propagation www.prolearninghub.com

Sample Exercise #1 What type of exception is divide by zero is ? This is an athematic exception . www.prolearninghub.com