Financial Engineering Project Course - PowerPoint PPT Presentation

About This Presentation
Title:

Financial Engineering Project Course

Description:

Summer A-2000, Project Course ... exceptions with the try, catch, and throw keywords. exception handler ... program can recover from, the program can resume ... – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 27
Provided by: mm77
Category:

less

Transcript and Presenter's Notes

Title: Financial Engineering Project Course


1
Financial Engineering Project Course
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
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. exceptio
n 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
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
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
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
General Form
An exception may be thrown from inside the try
block.
try statement(s)
catch (exceptiontype name)
statement(s) finally
statement(s)
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
The FixedFloatSwap.dtd
lt?xml version"1.0" encoding"utf-8"?gt lt!ELEMENT
FixedFloatSwap (Notional, Fixed_Rate, NumYears,

NumPayments) gt lt!ELEMENT Notional (PCDATA)
gt lt!ELEMENT Fixed_Rate (PCDATA) gt lt!ELEMENT
NumYears (PCDATA) gt lt!ELEMENT NumPayments
(PCDATA) gt
9
An Invalid Agreement.xml file
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
FixedFloatSwap SYSTEM "FixedFloatSwap.dtd"gt ltFixed
FloatSwapgt ltNotionalgt100lt/Notionalgt
ltFixed_Rategt5lt/Fixed_Rategt ltNumYearsgt3lt/NumYear
sgt ltNumPaymentsgt6lt/NumPaymentsgt
ltNotionalgt100lt/Notionalgt lt/FixedFloatSwapgt
A second notional?
Validation errors do not necessarily cause
exceptions.
10
Validating the Agreement.xml file
// Imports as before public static void
main(String args)
11
Validating the Agreement.xml file
try DocumentBuilderFactory
docBuilderFactory
DocumentBuilderFactory.newInstance()
docBuilderFactory.set
Validating(true)
docBuilderFactory.setNamespaceAware(true)
DocumentBuilder docBuilder
docBuilderFactory.newD
ocumentBuilder()
This factory will produce parsers that validate!
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(argv0))
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
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(argv0))
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
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
Vanilla Fixed-Float Swap
  • See the file vanillaFixedFloat.xml from fpml.org
  • See the file vanillaFixedFloat.dtd from fpml.org

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
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
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
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
InputSource is new InputSource("http//hemp
el.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
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
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
public void privatePrint(Node n, int indent)
for(int i 0 i lt 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("lt?"n.getNodeName()"...?gt"
"...PI Node") break

24
case n.COMMENT_NODE
System.out.println("lt!--"n.getNodeValue()"--gt"
"...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.getNod
eName() "...Document Type Node")
break default
System.out.println("?" n.getNodeName())
Node child n.getFirstChild()
while(child ! null)
privatePrint(child, indentcurrentIndent)
child child.getNextSibling()

25
See the Output of the programin Results.txt
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.
Write a Comment
User Comments (0)
About PowerShow.com