Exception的几个方法:
package ppp;
import java.lang.Exception;
public class start {
public static void main(String args[]) {
try {
throw new Exception("my exception");
}catch(Exception e) {
System.out.println("e.getMessage():"+e.getMessage());
System.err.println("e.toString():"+e);
System.err.println("e.printStackTrace():");
e.printStackTrace();
}
}
}
package dsm;
import java.io.*;
import java.util.Scanner;
/** Lab 13. CS1110 Exception handling */
class Lab13 {
/** = a buffered reader attached to the keyboard. If you store this value
* in a variable kbd (say), then
*
* kbd.readLine()
*
* will prompt the reader to type a line by putting in the Interactions pane
* a field into which to type and will then yield the string of characters
* the user typed in.
*/
public static BufferedReader getKeyboard() {
// Create a link to the keyboard
return new BufferedReader(new InputStreamReader(System.in));
}
/** Prompt the reader to type a line into the interactions pane and
* return the line that the user types. */
public static String readKeyboardLine() throws IOException {
BufferedReader kyboard= getKeyboard();
String line= kyboard.readLine();
return line;
}
/** Prompt the reader to type an integer (an int) into the interactions pane
* and return the integer that they type. If the user types something that
* is not an int, then issue a message (System.out.println(...) and prompts
* again.
*/
public static int readKeyboardInt() throws IOException {
BufferedReader kyboard= getKeyboard();
String stream=kyboard.readLine();
try {
Integer.parseInt(stream);
}catch(Exception e) {
throw new IOException("输入格式错误,请您输入*整数*");
}
return 0;
}
/** = b**c.
Precondition: c ?0
*/
public static double exp(double b, int c) throws IOException {
if(c<0) {
IOException e1=new IOException("输入错误:c<0");
throw e1;
}
if (c == 0)
return 1;
// c > 0
if (c%2 == 0)
return exp(b*b, c / 2);
// c is odd and > 0
return b * exp(b, c-1);
}
/** = the value i such that x**i <= .00000001 but x**(i-1) is not.
Throw MyException if x <= 0 or 1 <= x.
* @throws MyException
* @throws IOException
*/
public static int approach(double x) throws MyException, IOException {
int i= 1; // stub; you have to complete this function
if(x<=0) {throw new MyException("MyException: x<=0");}
else if(x>=1) {throw new MyException("MyException: x>=1");}
else {
while(true) {
if(exp(x, i)<0.1)break;
i++;
}
return i;
}
}
}
class MyException extends Exception{
public MyException() {
}
public MyException(String str) {
super(str);
}
}
public class Testdzk {
public static void main(String args[]) throws MyException, IOException {
Lab13 test=new Lab13();
System.out.println("***Case 1:判断输入的字符是否为数字***");
while(true){
try {
System.out.println("请输入一个整数:");
test.readKeyboardInt();
break;
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
System.out.println("恭喜您,输入成功!");
System.out.println("\n***Case 2:判断c是否小于0***");
Scanner sc=new Scanner(System.in);
while(true){
try {
System.out.println("请输入c:");
int c=sc.nextInt();
test.exp(1, c);
break;
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
System.out.println("恭喜您,输入成功!");
System.out.println("\n***Case 3:测试异常类+测试approach***");
while(true){
try {
System.out.println("请输入x:");
double x=sc.nextFloat();
System.out.println(test.approach(x));
System.out.println("恭喜您,计算成功!");
break;
} catch (MyException e2) {
System.out.println(e2.getMessage());
}
}
}
}