Slides Credit Umair Javed LUMS Web Application Development.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
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.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
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:
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.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Preventing and Correcting Errors
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
COMP Exception Handling Yi Hong June 10, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
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.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Object Throwable ErrorException RuntimeException.
Java Exceptions a quick review….
Introduction to Exceptions in Java
Introduction to Exceptions in Java
Chapter 12 Exception Handling
Web Design & Development Lecture 7
Exceptions 10-May-19.
Java Basics Exception Handling.
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

Slides Credit Umair Javed LUMS Web Application Development

There are two ways to write error-free programs; only the third one works

 Three types of error:  Syntax Errors – arise because the rules of the language are not followed.  Runtime Errors – arise because the program tries to perform an operation that is impossible to carry out.  Logic Errors – arise because the program does perform the way it was intended to.  Syntax errors are caught by the compiler, and fixed before the program is run.  Logic Errors are detected by testing, and are fixed through debugging.  Runtime Errors cause Exceptions and may be handled at runtime.

 An exception is an event that describes an unusual or erroneous situation at runtime.  Exceptions are wrapped up as objects  A program can deal with an exception in one of three ways:  i gnore it  handle it where it occurs  handle it an another place in the program  An error is also represented as an object in Java, but usually represents an unrecoverable situation and should not be caught

 Uses of exception handling  Process exceptions from program components  Handle exceptions in a uniform manner in large projects  Remove error-handling code from “main line” of execution  What if Exception is not handled?  Might terminate program execution

 Two Types  Unchecked ▪ Subclasses of RuntimeException and Error. ▪ Does not require explicit handling ▪ Run-time errors are internal to your program, so you can get rid of them by debugging your code ▪ For example, null pointer exception; index out of bounds exception; division by zero exception;...

 Two Types  Checked ▪ Must be caught or declared in a throws clause ▪ Compile will issue an error if not handled appropriately ▪ Subclasses of Exception other than subclasses of RuntimeException. ▪ Other arrive from external factors, and cannot be solved by debugging ▪ Communication from an external resource – e.g. a file server or database

Throwable ExceptionError IOException ArrayIndexOutOfBoundsExceptionInputMismatchException NullPointerException Runtime Exception AWTErrorThreadDeath ClassCastException OutOf MemoryError ArithmeticException UnChecked Exceptions Checked Exceptions Errors

 Basic Java exception handling is managed via keywords: try, catch, finally, throw, throws.  try block  Code that could generate errors put in try blocks  catch block  Code for error handling enclosed in a catch clause  finally block  The finally clause always executes  Resources that are opened may need to be closed during exception handling  Appears after the last catch block  It wil not execute if System.exit(0) occurs first

 throw  To manually throw an exception, use the keyword throw.  throws  throws exception out of the method, requiring it to be caught and handled by an appropriate exception handler  Any exception that is thrown out of a method must be specified as such by a throws clause.

try // try block { // write code that could generate exceptions } catch ( ) // catch block { // write code for exception handling } …… catch ( ) // catch block { //code for exception handling } finally // finally block { // any clean-up code, release the acquired resources }

 Possible to have multiple catch clauses for a single try statement [  Essentially checking for different types of exceptions that may happen  Evaluated in the order of the code  Bear in mind the Exception hierarchy when writing multiple catch clauses!  If you catch Exception first and then IOException, the IOException will never be caught!

 Method doesn’t want to handle exception itself  it throws the exception, the caller should handle this exception or throws the exception itself  A method should specify the exceptions it throws by placing a throws clause after the parameter list

 When dealing with exceptions  Especially when debugging  printStackTrace() will:  Show you the full calling history  With line numbers  Note:  Bad idea to eat an exception silently!  Either printStackTrace() or pass it along to be handled at a different level

 Method throws back the exception  No catch blocks matches  Exception is not handled Java runtime system main method calls main method1 calls method1 method2 calls method2 exception if not caught or throws back if not caught or throws back if not caught or throws back