Yoshi

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

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.
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. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
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 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.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
COMP 121 Week 5: Exceptions and Exception Handling.
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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
COP INTERMEDIATE JAVA Exception Handling Serialization.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Handling Errors with Exception (in Java) Project 10 CSC 420.
Java Exception Handling ● Exception = an event that occurs during the execution of a program that disrupts the normal flow of instructions: – Examples:
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
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.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
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.
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.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
CS 2511 Fall  Exception = an event that occurs during the execution of a program that disrupts the normal flow of instructions:  Examples: Out.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
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.
Practice Session 9 Exchanger CyclicBarrier Exceptions.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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:
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
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 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
Exceptions handling Try, catch blocks Throwing exceptions.
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 Basics Exception Handling.
Exception Handling.
Presentation transcript:

Yoshi

 An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

 A program is going to read a file, but it is missing  A program is reading an array, but the out of bound case occurs  A program is receiving a network packet, but the connection fails  JVM crashes  Are the cases above all exceptions?

 When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error.  Simple programs typically do not catch or throw Errors.

 Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not a serious system problem.  Most programs you write will throw and catch Exceptions as opposed to Errors.

 Checked exception  These are exceptional conditions that a well-written application should anticipate and recover from.  Unchecked exceptions  Error  These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.  Runtime exception  These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.  These usually indicate programming bugs, such as logic errors or improper use of an API

 Divided by zero  Stack overflow  I/O exceptions, such as packet lost  Disk writing error  Out of memory

 To those checked exceptions, we have to consider the case that exception happens private Vector vector; private static final int SIZE = 10; PrintWriter out = null; try { System.out.println("Entered try statement"); out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) { out.println("Value at: " + i + " = " + vector.elementAt(i)); } catch and finally statements...

try { //maybe read a file or something… } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); throw new SampleException(e); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } //Why we catch FileNotFoundException first, and then IOException?

 io/FileNotFoundException.html io/FileNotFoundException.html java.io Class FileNotFoundException java.lang.Object - java.lang.Throwable - java.lang.Exception - java.io.IOException - java.io.FileNotFoundException

 The finally block always executes when the try block exits.  The finally block is useful for more than just exception handling  it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break.  Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

public class TestFinally { private void m1() { System.out.println("Before entering m2()"); m2(); System.out.println("After exiting m2()"); } private void m2() { try { System.out.println("In m2()"); return; } finally { System.out.println("In m2's finally"); } public static void main(String[] args) { new TestFinally().m1(); }

 The finally block is a key tool for preventing resource leaks.  When closing a file or otherwise recovering resources, place the code in a finally block to insure that resource is always recovered.

try { //…something might have exception } catch (SomeExceptionClass e) { //handle the exception here } finally { //recover resources } What is something?

 /FileInputStream.html#read(byte[]) /FileInputStream.html#read(byte[])

 When we want others to “handle the possible exception”, add the “throws” keyword to the method  Tell others to “catch the exception which I throw”

public class ThrowExceptionExample { private void m1() { m2(); } private void m2() throws Exception { //I just want to throw an exception throw new Exception(); } public static void main(String[] args) { new ThrowExceptionExample().m1(); }

C:\javasrc>javac ThrowExceptionExample.java ThrowExceptionExample.java:4: unreported exception java.lang.Exception; must be caught or declared to be thrown m2(); ^ 1 error C:\javasrc>

public class ThrowExceptionExample { private void m1() { try { m2(); } catch(Exception e) { System.out.println("I catch it!"); } private void m2() throws Exception { //I just want to throw an exception throw new Exception(); } public static void main(String[] args) { new ThrowExceptionExample().m1(); }

 You can throw multiple exceptions  private void m2() throws Exception, ArrayIndexOutOfBoundsException  You can throw runtime exception  Remember that ArrayIndexOutOfBoundsException is an unchecked exception; including it in the throws clause is not mandatory.  That is, you can skip it  You can define your own “Throwable class”  Extend java.lang.Throwable, or java.lang.Exception  Suggestions  If a client can reasonably be expected to recover from an exception, make it a checked exception.  If a client cannot do anything to recover from the exception, make it an unchecked exception.

 The try block identifies a block of code in which an exception can occur.  The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception.  The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block.

 ntial/exceptions/QandE/questions.html ntial/exceptions/QandE/questions.html