Exception handling in Java. J. Pöial

Similar documents
Algorithms and Data Structures

Lecture 19 Programming Exceptions CSE11 Fall 2013

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.

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

Java Basics: Part 3 - Exceptions. Manuel Oriol

Java Programming Language Mr.Rungrote Phonkam

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

National University. Faculty of Computer Since and Technology Object Oriented Programming

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

Errors and exceptions in Java E

CSC 1214: Object-Oriented Programming

BBM 102 Introduction to Programming II Spring Exceptions

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

BBM 102 Introduction to Programming II Spring 2017

Study Notes for Sun Certified Programmer for Java 2 Platform

Data Structures. 02 Exception Handling

More on Exception Handling

EXCEPTION HANDLING. Summer 2018

ECE 122. Engineering Problem Solving with Java

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Errors and Exceptions

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

More on Exception Handling

Exceptions. CSC207 Winter 2017

Chapter 13 Exception Handling

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

Object-Oriented Programming in the Java language

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

ITI Introduction to Computing II

ITI Introduction to Computing II

Correctness and Robustness

For more details on SUN Certifications, visit

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

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

Unit 4. Exception handling mechanism. new look try/catch mechanism in Java Enumeration in Java 5 - usage.

UNIT - V. Inheritance Interfaces and inner classes Exception handling Threads Streams and I/O

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

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

EXCEPTIONS. Java Programming

C16b: Exception Handling

Writing usable APIs in practice

COMP-202 Unit 9: Exceptions

Exceptions. James Brucker

Fundamentals of Object Oriented Programming

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

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Projeto de Software / Programação 3 Tratamento de Exceções. Baldoino Fonseca/Márcio Ribeiro

Programming - 2. Common Errors

Programming II (CS300)

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

IS311 Programming Concepts 2/59. AVA Exception Handling Jการจ ดการส งผ ดปรกต

Exception Handling CSCI 201 Principles of Software Development

School of Informatics, University of Edinburgh

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Exception Handling. Exception Handling


Writing usable APIs in practice

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

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

QUESTION BANK. SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING

Programming Languages and Techniques (CIS120)

Full file at Chapter 2 - Inheritance and Exception Handling

CIS265 - Spring Exam 2 First Name Last Name CSU#

Exceptions Handling Errors using Exceptions

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

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

Chapter 12 Exception Handling

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

Introductory Programming Exceptions and I/O: sections

CS159. Nathan Sprague

Why Exceptions? (1.1) Exceptions. Why Exceptions? (1.2) Caller vs. Callee. EECS2030 B: Advanced Object Oriented Programming Fall 2018

Exceptions. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

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

CSE 143 Java. Exceptions 1/25/

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Exceptions and Error Handling

COMP-202 Unit 9: Exceptions

Exception Handling. Throw. Object. Run-time Errors

17. Handling Runtime Problems

Index COPYRIGHTED MATERIAL

Programming II (CS300)

Software Practice 1 - Error Handling

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

SECTION-1 Q.1.Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

More fun with Java. Packages. Simple Declarations. Import. Basic Structure of a Java Program. /* a comment */ // a comment

Java Exception. Wang Yang

School of Informatics, University of Edinburgh

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

Java Errors and Exceptions. Because Murphy s Law never fails

Introduction Unit 4: Input, output and exceptions

Unit III Rupali Sherekar 2017

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

What are Exceptions?

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

File I/O and Exceptions

Chapter 12 Exception Handling and Text IO. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Transcription:

Exception handling in Java J. Pöial

Errors and exceptions Error handling without dedicated tools: return codes, global error states etc. Problem: it is not reasonable (or even possible) to handle each unusual situation in the same place (subroutine) it occurs. How to separate error handling from the normal control flow?

Errors and exceptions In Java: try / catch (control statement) Throwable (specialized objects) Error (program cannot continue) Exception (unusual situation) RuntimeException (no obligation to catch) throw (raise exception) throws (method heading - delegating)

Errors Error LinkageError ClassCircularityError ClassFormatError IncompatibleClassChangeError NoSuchMethodError NoSuchFieldError InstantiationError AbstractMethodError IllegalAccessError NoClassDefFoundError VerifyError AbstractMethodError ExceptionInInitializationError

Errors ThreadDeath VirtualMachineError InternalError OutOfMemoryError StackOverflowError UnknownError AWTError

Checked exceptions Exception ClassNotFoundException CloneNotSupportedException IllegalAccessException InstantiationException InterruptedException NoSuchMethodException TooManyListenersException ParseException AWTException

IOException IOException CharConversionException EOFException FileNotFoundException InterruptedIOException ObjectStreamException InvalidClassException InvalidObjectException NotActiveException NotSerializableException OptionalDataException StreamCorruptedException WriteAbortedException

IOException SyncFailedException UnsupportedEncodingException UTFDataFormatException MalformedURLException ProtocolException SocketException BindException ConnectException NoRouteToHostException UnknownHostException UnknownServiceException

RuntimeException RuntimeException ArithmeticException ArrayStoreException ClassCastException IllegalArgumentException IllegalThreadStateException NumberFormatException FormatException IllegalMonitorStateException IllegalStateException IndexOutOfBoundsException ArrayIndexOutOfBoundsException StringIndexOutOfBoundsException

RuntimeException NegativeArraySizeException NullPointerException SecurityException EmptyStackException MissingResourceException NoSuchElementException IllegalComponentStateException

try / catch try { block where exceptions may occur; } catch (ExcType_1 variable) { trap_1; }... catch (ExcType_n variable) { trap_n; } finally { epilogue; }

Example try { FileInputStream p = new FileInputStream ("/etc/passwd"); byte[] sisu = new byte [p.available()]; p.read (sisu); p.close(); System.out.write (sisu); } catch (FileNotFoundException e) { System.out.println ("File not found " + e); } catch (IOException e) { System.out.println ("Input/Output error " + e); } catch (Exception e) { System.out.println ("Something unusual happened " + e); } finally { System.out.println (" This is finally branch"); } // try

throw statement To raise an exception in your program throw throwableobject; Usually error message is provided throw new SecurityException ("No permission to read!"); All checked (not RuntimeExceptions) exceptions need handling try/catch or delegation "up" using exception declaration in method heading

throws declaration public static void pause() throws InterruptedException { Thread.sleep (1000); } public Object nextelement() throws java.util.nosuchelementexception { if (pointertonext() == null) throw new } java.util.nosuchelementexception(); else return pointertonext(); Corresponding javadoc tag!

Problems When extending existing exception class provide both default constructor (with no parameters) and a constructor with String parameter (error message). No "resume" use loop structures to continue execution Declare needed variables before try-block, otherwise they are not accessible in traps (catch branches)

Examples Chaining Resume and other things