Tirgul 13 Exceptions 1.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Yoshi
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.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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
Preventing and Correcting Errors
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.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
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.
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.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
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
Binary trees Sorting Exceptions
Chapter 10 – Exception Handling
Chapter 13 Exception Handling
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Chapter 12 Exception Handling and Text IO
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Advanced Programming Behnam Hatami Fall 2017.
EE422C Software Implementation II
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 12 Exception Handling
Abdulmotaleb El Saddik University of Ottawa
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Lecture 9: Exceptions in Java CS201j: Engineering Software
Web Design & Development Lecture 7
Exception Handling in Java
Chapter 13 Exception Handling
Chapter 12: Exceptions and Advanced File I/O
Java Exceptions Dan Fleck CS211.
Exceptions 25-Apr-19.
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.
Tutorial Exceptions Handling.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exceptions 10-May-19.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Lab 1 Exception Handling.
Presentation transcript:

Tirgul 13 Exceptions 1

Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary

Introduction Users have high expectations for the code we produce. Users will use our programs in unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected ways during execution

Introduction It is our responsibility to produce quality code that does not fail unexpectedly. Consequently, we must design error handling into our programs.

Errors and Error Handling An Error is any unexpected result obtained from a program during execution. Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination. Errors should be handled by the programmer, to prevent them from reaching the user.

Errors and Error Handling Some typical causes of errors: Memory errors (i.e. memory incorrectly allocated, memory leaks, “null pointer”) File system errors (i.e. disk is full, disk has been removed) Network errors (i.e. network is down, URL does not exist) Calculation errors (i.e. divide by 0)

Errors and Error Handling More typical causes of errors: Array errors (i.e. accessing element –1) Conversion errors (i.e. convert ‘q’ to a number) Can you think of some others?

Exceptions What are they? An exception is a representation of an error condition or a situation that is not the expected result of a method. Exceptions are built into the Java language and are available to all program code. Exceptions isolate the code that deals with the error condition from regular program logic.

Exceptions Exceptions fall into two categories: Checked Exceptions Unchecked Exceptions Checked exceptions are inherited from the core Java class Exception. They represent exceptions that are frequently considered “non fatal” to program execution Checked exceptions must be handled in your code (catch it), or passed to calling code for handling (declare that your method might throw it using the throws keyword at the method signature).

Exceptions Unchecked exceptions represent error conditions that are considered “fatal” to program execution. They all inherit from the RuntimeException class. You are not forced to catch, or declare that you throw, an unchecked exception (but you’re allowed to, if you want). If an exception is not caught, it will eventually crash the program and a stack-trace will be printed.

Exceptions Examples: Checked exceptions include errors such as “file not found” and “number format conversion”. Unchecked exceptions include errors such as “null pointer”.

Exceptions How do you handle exceptions? Exception handling is accomplished through the “try – catch” mechanism, or by a “throws” clause in the method declaration. For any code that throws a checked exception, you can decide to handle the exception yourself, or pass the exception “up the chain” (to the “parent” code that called your function).

Exceptions How do you handle exceptions? To handle the exception, you write a “try-catch” block. To pass the exception “up the chain”, you declare a throws clause in your method or class declaration. If the method contains code that may cause a checked exception, you MUST handle the exception OR pass the exception to the calling code context.

Coding Exceptions Try-Catch Mechanism Wherever your code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword: After the try block, the code to handle the exception should it arise is placed in a block of code starting with the “catch” keyword.

Exception Handling in Java Coding Exceptions Try-Catch Mechanism You may also write an optional “finally” block. This block contains code that is ALWAYS executed, either after the “try” block code, or after the “catch” block code. Finally blocks can be used for operations that must happen no matter what (i.e. cleanup operations such as closing a file) June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Example try { … normal program code } catch(Exception e) { … exception handling code } June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Passing the exception In any method that might throw an exception, you may declare that the method “throws” that exception, and thus avoid handling the exception yourself Example public void myMethod throws IOException { … normal code with some I/O } June 14, 2001 Exception Handling in Java

Coding Exceptions Types of Exceptions All checked exceptions have class “Exception” as the parent class. You can use the actual exception class or the parent class when referring to an exception Where do you find the exception classes? Reference books such as “Java in a Nutshell” (O’Reilly, 2001), or the Java Documentation. Various online resources…

JAVA Exception hierarchy Blue: checked exceptions Red: Unchecked exceptions

Example: catch general exception try{ int[] myArray=null; System.out.println(myArray[3]); } catch(Exception e){ System.out.println(e.getMessage()); e.printStackTrace(); NullPointerException !

Example: several catch statements try{…} catch(NullPointerException npe){ System.out.println(“npe"); } catch(Exception e){ System.out.println("got Exception"); The order in which exceptions are caught is the order in which the code is written. If we caught Exception, and only then NullPointerException, the code would fail to compile!

Checked exceptions must be checked or propagated! class Example2{ public static void test (){ try{ int message=System.out.read(); System.out.pribtln("successful read"); } Catch (IOEXception exception){ System.out.println("unsuccessful read");   class Example2{ public static void test () throws IOException{ int message=System.out.read(); System.out.pribtln("successful read"); }

Writing a new exception public class MyException extends Exception} public MyException (String error){ super(error); } { public static void foo(int n) throws MyException{ if (n<5) throw new MyException(“n could not be <5 in foo”); System.out.println(“Thank you and have a good day”);

Using the execption int n = (int)(Math.random()*10); try{ foo(n); } catch (MyException e){ e.printStackTrace();

Summary Exceptions are a powerful error handling mechanism. Exceptions in Java are built into the language. Exceptions can be handled by the current code (try-catch), or handled by the caller (throws).