Printstacktracemethoddenedonexcepions

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5 1/28/10 Try-catch-blocks •  The basic syntax: try { // statements that may throw excepIons } catch(ExcepIon e) { // usually a “subclass” // an excepIon was thrown // try to handle the excepIon } // if we don’t exit from the catch statement, we keep going •  Things can get more complicated than this CPSC 324 ‐‐ Spring 2010 11 Handling Exceptions in Try-catch-blocks •  Sometimes, print an error and exit program •  Often in practice, handle by ignore and continue •  … but it is good practice to print the stack trace: try { // statements that may throw excepIons } catch(ExcepIonType e) { e.printStackTrace(); // method defined on excepIons } // keep going as if nothing happened (if possible) •  In the previous (unhandled) exception, the JVM printed the stack trace for us CPSC 324 ‐‐ Spring 2010 12 6 1/28/10 Basic File I/O •  To write text to a file … File aFile = new File(“out.txt ”); // output file PrintWriter out = new PrintWriter(aFile); out.println(“one fish two fish red fish blue fish”); out.flush(); // flush the stream to the file out.close(); // close the stream •  Note that this overwrites the file … •  We’ll talk more about writing to files later CPSC 324 ‐‐ Spring 2010 13 Enumerated Types •  For representing a finite domain of “coded” values enum Size {...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 324 at Gonzaga.

Ask a homework question - tutors are online