Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

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.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Week 5 Using snoop.jsp under Jigsaw Using JavaBeans with JSP Instance variables and local variables An application using JSP, JavaBeans, and FpML An application.
Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
Financial Engineering Project Course. Lecture 3 Object Oriented Design Inheritance Abstract Base Classes Polymorphism Using XML to represent the swap.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
OCT1 Java and XML (DOM and SAX) Some of the material for these slides came from the following sources: “XML a Manager’s Guide” by Kevin Dick “The XML Companion”
Internet Technologies Java and XML (DOM and SAX) Some of the material for these slides came from the following sources: “XML a Manager’s Guide” by Kevin.
Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Internet Technologies1 Java and XML (DOM and SAX) Some of the material for these slides came from the following sources: “XML a Manager’s Guide” by Kevin.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Exceptions COMPSCI 105 S Principles of Computer Science.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Object Oriented Programming
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 Science [3] Java Programming II - Laboratory Course Lab 3-1: Creating and Using Interfaces Exception Faculty of Engineering & IT Software Engineering.
1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.
The Java Programming Language
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Schema Data Processing
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Error Handling Tonga Institute of Higher Education.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
COP 3330 Notes 3/7. Today’s Topics Exceptions Abstract Classes.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Lecture 18B Exception Handling and Richard Gesick.
Chapter 10 – Exception Handling
Java Programming Language
Testing and Exceptions
ATS Application Programming: Java Programming
Chapter 12 Exception Handling and Text IO
Exception Handling in Java
Lecture 11 Objectives Learn what an exception is.
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 Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course

Summer A-2000, Project Course-- Carnegie Mellon University 2 Lecture 6 Java Exceptions (Definitions from JavaSoft) Validation revisited A “Real” FpML document – Vanilla Fixed- Float Swap A “Real” FpML DTD – fpml.dtd Getting the document and dtd from a server in Java Results

Summer A-2000, Project Course-- Carnegie Mellon University 3 Exceptions--Definitions exception An event during program execution that prevents the program from continuing normally; generally, an error. The Java programming language supports exceptions with the try, catch, and throw keywords. exception handler A block of code that reacts to a specific type of exception. If the exception is for an error that the program can recover from, the program can resume executing after the exception handler has executed.

Summer A-2000, Project Course-- Carnegie Mellon University 4 Exceptions-- Definitions catch A Java programming language keyword used to declare a block of statements to be executed in the event that a Java exception, or run time error, occurs in a preceding "try" block. throw A Java programming language keyword that allows the user to throw an exception or any class that implements the "throwable" interface.

Summer A-2000, Project Course-- Carnegie Mellon University 5 Exceptions-- Definitions throws A Java programming language keyword used in method declarations that specify which exceptions are not handled within the method but rather passed to the next higher level of the program. try A Java programming language keyword that defines a block of statements that may throw a Java language exception. If an exception is thrown, an optional "catch" block can handle specific exceptions thrown within the "try" block. Also, an optional "finally" block will be executed regardless of whether an exception is thrown or not.

Summer A-2000, Project Course-- Carnegie Mellon University 6 Exceptions-- Definitions finally A Java programming language keyword that executes a block of statements regardless of whether a Java Exception, or run time error, occurred in a block defined previously by the "try" keyword.

Summer A-2000, Project Course-- Carnegie Mellon University 7 General Form try { statement(s) } catch (exceptiontype name) { statement(s) } finally { statement(s) } An exception may be thrown from inside the try block. These statements are executed if an exception of a particular type occurs within the try block These statements are executed regardless of whether or not an error occurs within the try block.

Summer A-2000, Project Course-- Carnegie Mellon University 8 The FixedFloatSwap.dtd <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) >

Summer A-2000, Project Course-- Carnegie Mellon University 9 An Invalid Agreement.xml file A second notional? Validation errors do not necessarily cause exceptions.

Summer A-2000, Project Course-- Carnegie Mellon University 10 Validating the Agreement.xml file // Imports as before public static void main(String args[]) { :

Summer A-2000, Project Course-- Carnegie Mellon University 11 Validating the Agreement.xml file try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); This factory will produce parsers that validate!

Summer A-2000, Project Course-- Carnegie Mellon University 12 docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException {} public void error(SAXParseException e) throws SAXParseException { throw e; } public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } ); Document doc1 = docBuilder.parse(new File(argv[0])); Validation errors force a call on this method. The method may or may not throw an exception. This method has chosen to throw an exception. If it does not throw one then the catch clause does not execute. docBuilder makes use of the ErrorHandler object

Summer A-2000, Project Course-- Carnegie Mellon University 13 docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException {} public void error(SAXParseException e) throws SAXParseException { throw e; } public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } ); Document doc1 = docBuilder.parse(new File(argv[0])); Executed if the.dtd file is not found or “SYSTEM” is not spelled correctly, etc. Unlike error, after fatalError is called an exception IS thrown by the parser.

Summer A-2000, Project Course-- Carnegie Mellon University 14 : catch(SAXParseException err) { System.out.println("Catching raised exception"); System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } This catch clause catches the exception and the program terminates by finishing main.

Summer A-2000, Project Course-- Carnegie Mellon University 15 Vanilla Fixed-Float Swap See the file vanillaFixedFloat.xml from fpml.org See the file vanillaFixedFloat.dtd from fpml.org

Summer A-2000, Project Course-- Carnegie Mellon University 16 A Java Program that reads fpml from a server and performs validation against the server based DTD. The program then displays the fpml file by traversing the DOM tree.

Summer A-2000, Project Course-- Carnegie Mellon University 17 import org.xml.sax.*; public class Simulator6 { public static void main(String argv[]) { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Summer A-2000, Project Course-- Carnegie Mellon University 18 docBuilder.setErrorHandler( new org.xml.sax.ErrorHandler() { public void fatalError(SAXParseException e) throws SAXException { System.out.println("Fatal error"); // an exception will be thrown } public void error(SAXParseException e) throws SAXParseException { System.out.println("Validity error"); throw e; }

Summer A-2000, Project Course-- Carnegie Mellon University 19 public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); throw err; } );

Summer A-2000, Project Course-- Carnegie Mellon University 20 InputSource is = new InputSource(" Document doc = docBuilder.parse(is); System.out.println("No Problems found"); TreePrinter tp = new TreePrinter(doc); tp.print(); }

Summer A-2000, Project Course-- Carnegie Mellon University 21 catch(SAXParseException err) { System.out.println("Catching raised exception"); System.out.println("Parsing error" + ", line " + err.getLineNumber() + ", URI " + err.getSystemId()); System.out.println(" " + err.getMessage()); } catch(SAXException e) { System.out.println("Catch clause 2"); Exception x = e.getException(); ((x == null) ? e : x).printStackTrace(); } catch (Throwable t) { System.out.println("Catch clause 3"); t.printStackTrace(); } System.exit(0); }

Summer A-2000, Project Course-- Carnegie Mellon University 22 TreePrint Class import org.w3c.dom.*; public class TreePrinter { private Document doc; private int currentIndent; public TreePrinter(Document d) { currentIndent = 2; doc = d; } public void print() { privatePrint(doc,currentIndent); }

Summer A-2000, Project Course-- Carnegie Mellon University 23 public void privatePrint(Node n, int indent) { for(int i = 0; i < indent; i++) System.out.print(" "); switch( n.getNodeType()) { // Print information as each node type is encountered case n.DOCUMENT_NODE : System.out.println(n.getNodeName() + "...Document Node"); break; case n.ELEMENT_NODE : System.out.println(n.getNodeName() + "...Element Node"); break; case n.TEXT_NODE : System.out.println(n.getNodeName() + "...Text Node"); break; case n.CDATA_SECTION_NODE: System.out.println(n.getNodeName() + "...CDATA Node"); break; case n.PROCESSING_INSTRUCTION_NODE: System.out.println(" "+ "...PI Node"); break;

Summer A-2000, Project Course-- Carnegie Mellon University 24 case n.COMMENT_NODE: System.out.println(" " + "...Comment node"); break; case n.ENTITY_NODE: System.out.println("ENTITY "+ n.getNodeName()+ "...Entity Node"); break; case n.ENTITY_REFERENCE_NODE: System.out.println("&"+n.getNodeName()+";" + "...Entity Reference Node"); break; case n.DOCUMENT_TYPE_NODE: System.out.println("DOCTYPE"+n.getNodeName()+ "...Document Type Node"); break; default: System.out.println("?" + n.getNodeName()); } Node child = n.getFirstChild(); while(child != null) { privatePrint(child, indent+currentIndent); child = child.getNextSibling(); }

Summer A-2000, Project Course-- Carnegie Mellon University 25 See the Output of the program in Results.txt

Summer A-2000, Project Course-- Carnegie Mellon University 26 Homework Read and validate the vanillaFixedFloat.xml file from my server “hempel.heinz.cmu.edu”. Perform some simple financial processing on the data and print the results. Turn in all of the code and printouts by the end of class next week. The processing need not be difficult. Simply demonstrate that you can read, validate and compute with financial data from the web.