Download presentation
Presentation is loading. Please wait.
1
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course
2
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
3
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.
4
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.
5
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.
6
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.
7
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.
8
Summer A-2000, Project Course-- Carnegie Mellon University 8 The FixedFloatSwap.dtd <!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments) >
9
Summer A-2000, Project Course-- Carnegie Mellon University 9 An Invalid Agreement.xml file 100 5 3 6 100 A second notional? Validation errors do not necessarily cause exceptions.
10
Summer A-2000, Project Course-- Carnegie Mellon University 10 Validating the Agreement.xml file // Imports as before public static void main(String args[]) { :
11
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!
12
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
13
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.
14
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.
15
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
16
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.
17
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();
18
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; }
19
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; } );
20
Summer A-2000, Project Course-- Carnegie Mellon University 20 InputSource is = new InputSource("http://hempel.heinz.cmu.edu/fpml/vanillaFixedFloat.xml"); Document doc = docBuilder.parse(is); System.out.println("No Problems found"); TreePrinter tp = new TreePrinter(doc); tp.print(); }
21
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); }
22
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); }
23
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;
24
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(); }
25
Summer A-2000, Project Course-- Carnegie Mellon University 25 See the Output of the program in Results.txt
26
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.