- list, catch, or throw
- "All classes under the Exception class except for
the RunTimeException class and classes below it,
must be specified in a method's throws clause if
they are not handled by the method. These are called checked exceptions."
- using the call stack to trace the calling and handling sequence e.g. ExceptionDemo
- is part of the documentation
- Data Validation
public int readInt() throws IOException {
do {
try {
stdErr.print("Enter integer: ");
stdErr.flush();
return Integer.parseInt(stdIn.readLine());
}
catch (NumberFormatException nfe) {
stdErr.println(nfe.toString());
}
} while (true);
}
- Input Validation
import java.io.*;
public class InputValidation {
private static BufferedReader stdIn = new
BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdErr = new
PrintWriter(System.err, true);
public static int readInt(int lo, int hi) throws IOException {
int x;
do {
try {
stdErr.print("Enter integer: ");
stdErr.flush();
x = Integer.parseInt(stdIn.readLine());
if (x >= lo && x <= hi) {
break;
}
} catch (NumberFormatException nfe) {
stdErr.println(nfe.toString());
}
} while (true);
return x;
}
public static void main(String[] args) throws IOException {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println(readInt(a,b) + " in the range of " + a + " to " + b);
}
}
- FileNotFoundException
import java.io.*;
/**
* class DisplayTextFile
*
* This program prompts the user for a filename, reads in the filename,
* then reads and displays the contents on the console
*
* @author Terence Chan
* @version 1.0
*/
class DisplayTextFile {
private static BufferedReader stdIn = new
BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out, true);
private static PrintWriter stdErr = new PrintWriter(System.err, true);
/**
* prompt the user for a filename, read in the filename, open the file,
* then reads and displays the contents on the console
* @param args command line arguments
* @throws IOException If an input or output exception occurred
*/
public static void main(String[] args) throws IOException {
BufferedReader inFile = null;
// prompt the user for filename
do {
try {
stdOut.print("Enter Filename : ");
stdOut.flush();
inFile = new BufferedReader(new FileReader(stdIn.readLine()));
} catch (FileNotFoundException fnfe) {
stdErr.println(fnfe.toString());
break;
}
// read and display contents
String textLine;
textLine=inFile.readLine();
while (textLine!=null) {
stdOut.println(textLine);
textLine=inFile.readLine();
}
inFile.close();
break;
} while (true);
}
}
- MalformedURLException
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class ChoseSomeURL extends Applet implements
ItemListener {
Choice siteURL;
Label label;
String site[] = { "http://www.cityu.edu.hk/",
"http://www.yahoo.com.hk/" };
public void init() {
label = new Label("URL");
siteURL = new Choice();
// add items to siteURL list
for (int i = 0; i < site.length; i++)
siteURL.add(site[i]);
// add choice lists to window
add(label);
add(siteURL);
// register to receive item events
siteURL.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie) {
try {
URL url = new URL(siteURL.getSelectedItem());
getAppletContext().showDocument(url,"_blank");
} catch(MalformedURLException mue) {}
}
}
- IndexOutOfBoundsException
class SwapTwoIntWithValidation {
public static void main(String[] args) {
int temp, a = 0, b = 0;
try {
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
} catch (IndexOutOfBoundsException iobe) {
System.out.println(iobe.toString());
System.exit(0);
}
System.out.println("before swapping a and b : " + a + " " + b);
temp = a;
a = b;
b = temp;
System.out.println("after swapping a and b : " + a + " " + b);
}
}
- QuadraticSolver
class QuadraticSolver {
public static void main(String[] args) {
int a, b, c;
try {
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
if ((a == 0) && (b == 0))
throw new IllegalArgumentException("Invalid input: (a == 0) && (b == 0)");
if (b*b < 4*a*c)
throw new IllegalArgumentException("Invalid input: b*b < 4*a*c");
if (a == 0)
System.out.println("x = " + -c/b);
else {
System.out.println("x1 = " + ((-b + Math.sqrt(b*b-4*a*c))/(2*a)));
System.out.println("x2 = " + ((-b - Math.sqrt(b*b-4*a*c))/(2*a)));
}
} catch (IndexOutOfBoundsException iobe) {
System.out.println(iobe.toString());
}
catch (NumberFormatException nfe) {
System.out.println(nfe.toString());
}
catch (IllegalArgumentException iae) {
System.out.println(iae.toString());
}
catch (ArithmeticException ae) {
System.out.println(ae.toString());
}
}
}