Lab 1 Exception Handling.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Advertisements

Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
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.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
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.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
Chapter 13 Exception Handling
Introduction to Exceptions in Java
Chapter 12 Exception Handling and Text IO
CSE 501N Fall ’09 17: Exception Handling
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Advanced Java Programming
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Chapter 13 Exception Handling
Exceptions 19-Feb-19.
Java Exceptions Dan Fleck CS211.
Exceptions 7-Apr-19.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exceptions 10-May-19.
Why do we need exceptions?
Java Basics Exception Handling.
Exceptions 5-Jul-19.
Exception Handling.
Exceptions and Exception Handling
Presentation transcript:

Lab 1 Exception Handling

Lab 1: Exception Handling Introduction Exceptions in Java Types of Exceptions Handling Exceptions Causes of Exceptions

Introduction Exception handling is a mechanism for dealing with runtime errors. Syntactically-correct programs do not mean they are error-free during runtime. For example, a program trying to read data from a non-existent file. In languages such as C, the programmer is responsible for error checking but is not rigidly enforced by the language.

Java provides support for exception handling. The programmer is not forced to check whether f is null. For example (C), FILE *f = fopen("list.dat", "r"); fscanf(f, "%d", &qty); Java provides support for exception handling.

Exceptions in Java In Java, exceptions are represented as objects. An exception object representing a particular exception contains data about that exception. This makes it possible to obtain clues as to why it occurred and hopefully diagnose the cause of the problem.

When an exception occurs, an exception object is created and "thrown" to the calling method. The calling method may choose to handle ("catch") the exception and/or "throw" the exception to its calling method, and so on.

EXCEPTION OCCURS Method C throws the exception object Method C : Exception Method B throws the exception object calls Method B calls Method B catches the exception object and handles the exception Method A Method A catches the exception object and handles the exception

Each method involved in the calling chain must: provide error handling code (if the method wants to) for each type of exception object thrown by the methods it calls. rethrow the exception objects it receives to its calling method.

Types of Exceptions java.lang.Throwable java.lang.Error java.lang.Exception

java.lang.Error Exceptions These are serious errors which should be handled by the Java system. Examples of exception objects in this category: AWTError - serious error occurs in the AWT NoClassDefFoundError OutOfMemoryError

java.lang.RuntimeException Exceptions java.lang.RuntimeException is a subclass of java.lang.Exception. It usually represents fairly serious errors which are difficult to recover from. They are automatically handled by the Java system although the programmer may handle them himself if there is a need. Examples: NullPointerException, SecurityException

Handling Exception try and catch statements throws keyword Throwing an exception finally statement

try and catch statements … x.method(); } catch (ExceptionType e) { Calling a method that may throw an ExceptionType exception If the exception occurs, it will be caught here and statements in this block will be executed.

throws keyword A method may rethrow an exception without catching the exception. Example: public void method() throws IOException { … }

Throwing an Exception A method may catch an exception and rethrow it to its caller. Example: public void op() throws IOException { try { x.method(); } catch (IOException e) { System.out.println("Error"); throw e;

finally statement This statement is optional. Statements in the finally block will be executed whether or not an exception occurs. try { … } catch (ExceptionType e) { } finally { }