Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Similar documents
Software Practice 1 - Error Handling Exception Exception Hierarchy Catching Exception Userdefined Exception Practice#5

Software Practice 1 - Error Handling

6.Introducing Classes 9. Exceptions

Here is a hierarchy of classes to deal with Input and Output streams.

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

By: Abhishek Khare (SVIM - INDORE M.P)

BBM 102 Introduction to Programming II Spring Exceptions

Unit 5 - Exception Handling & Multithreaded

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

BBM 102 Introduction to Programming II Spring 2017

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

More on Exception Handling

More on Exception Handling

Sri Vidya College of Engineering & Technology Question Bank


Exceptions - Example. Exceptions - Example

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 14. Exception Handling and Event Handling ISBN

2- Runtime exception: UnChecked (execution of program) automatically propagated in java. don t have to throw, you can but isn t necessary

ITI Introduction to Computing II

G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

ITI Introduction to Computing II

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

What are Exceptions?

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

Exception Handling. --After creating exception object,method handover that object to the JVM.

EXCEPTIONS. Java Programming

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Exception-Handling Overview

EXCEPTION HANDLING. Summer 2018

ECE 122. Engineering Problem Solving with Java

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Exceptions and Libraries

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Exceptions. CSC207 Winter 2017

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

CSCI 261 Computer Science II

Full file at Chapter 2 - Inheritance and Exception Handling

Chapter 11 Handling Exceptions and Events. Chapter Objectives

16-Dec-10. Consider the following method:

13: Error Handling with Exceptions. The basic philosophy of Java is that "badly formed code will not be run."

Programming II (CS300)

Exceptions Handling Errors using Exceptions

Fundamentals of Object Oriented Programming

Object Oriented Programming

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

What can go wrong in a Java program while running?

Exception Handling in Java

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Introduction to Computer Science II CS S-22 Exceptions

Introduction to Software Design

CS159. Nathan Sprague

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Exception Handling CSCI 201 Principles of Software Development

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

9. Java Errors and Exceptions

Data Structures. 02 Exception Handling

CSC 1214: Object-Oriented Programming

Java Errors and Exceptions. Because Murphy s Law never fails

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Lecture 19 Programming Exceptions CSE11 Fall 2013

Correctness and Robustness

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

C16b: Exception Handling

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Chapter 12 Exception Handling

Exceptions and Error Handling

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

Object Oriented Programming

CS159. Nathan Sprague

Binary search. int index = Collections.binarySearch(list, element);

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

Exception Handling Generics. Amit Gupta

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

Chapter 13 Exception Handling

Object Oriented Programming Exception Handling

Java Loose Ends. 11 December 2017 OSU CSE 1

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

7. Java Input/Output. User Input/Console Output, File Input and Output (I/O)

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

2.6 Error, exception and event handling

Programming II (CS300)

Exception Handling. Chapter 11. Java By Abstraction Chapter 11. Outline What Are Exceptions?

Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

Programming Languages and Techniques (CIS120)

Transcription:

Exceptions, try - catch - finally, throws keyword JAVA Standard Edition

Java - Exceptions An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. An exception can occur for many different reasons. Following are some scenarios where an exception occurs: A user has entered an invalid data. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communications or the JVM has run out of memory.

Java - Exceptions Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. Based on these, we have three categories of Exceptions. You need to understand them to know how exception handling works in Java. Checked exceptions A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.

Example For example, if you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception: import java.io.file; import java.io.filereader; public class FilenotFound_Demo { public static void main(string args[]) { File file = new File("E://file.txt"); FileReader fr = new FileReader(file); OUTPUT: C:\>javac FilenotFound_Demo.java FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader fr = new FileReader(file); ^ 1 error

Unchecked exceptions Unchecked exceptions An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. public class Unchecked_Demo { public static void main(string args[]) { int num[] = {1, 2, 3, 4; System.out.println(num[5]); Output: Exception in thread "main" java.lang.arrayindexoutofboundsexception: 5 at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)

Exception Hierarchy All exception classes are subtypes of the java.lang.exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class. The Exception class has two main subclasses: IOException class and RuntimeException Class.

Exceptions Methods Sr.No. 1 2 3 4 5 6 Method & Description public String getmessage() Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor. public Throwable getcause() Returns the cause of the exception as represented by a Throwable object. public String tostring() Returns the name of the class concatenated with the result of getmessage(). public void printstacktrace() Prints the result of tostring() along with the stack trace to System.err, the error output stream. public StackTraceElement [] getstacktrace() Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack. public Throwable fillinstacktrace() Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.

Catching Exceptions A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following: try { // Protected code catch(exceptionname e1) { // Catch block

Example // File Name : ExcepTest.java import java.io.*; public class ExcepTest { public static void main(string args[]) { try { int a[] = new int[2]; System.out.println("Access element three :" + a[3]); catch(arrayindexoutofboundsexception e) { System.out.println("Exception thrown :" + e); System.out.println("Out of the block");

Catching Exceptions A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following: try { // Protected code catch(exceptionname e1) { // Catch block

Example import java.io.*; public class ExcepTest { public static void main(string args[]) { try { int a[] = new int[2]; System.out.println("Access element three :" + a[3]); catch(arrayindexoutofboundsexception e) { System.out.println("Exception thrown :" + e); System.out.println("Out of the block");

Multiple Catch Blocks A try block can be followed by multiple catch blocks. The syntax for multiple catch blocks looks like the following: try { // Protected code catch(exceptiontype1 e1) { // Catch block catch(exceptiontype2 e2) { // Catch block catch(exceptiontype3 e3) { // Catch block

The Throws/Throw Keywords If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature. You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the difference between throws and throw keywords, throws is used to postpone the handling of a checked exception and throw is used to invoke an exception explicitly.

Example import java.io.*; public class classname { public void deposit(double amount) throws RemoteException { // Method implementation throw new RemoteException(); // Remainder of class definition

The Finally Block The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception. Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code. try { // Protected code catch(exceptiontype1 e1) { // Catch block catch(exceptiontype2 e2) { // Catch block catch(exceptiontype3 e3) { // Catch block finally { // The finally block always executes.

Thank you for attention