抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

JAVA 实验8.2

题一:

package Try;

public class Test1 {

    public static void main(String[] args) {
        try {
            throw new Exception("demo exception");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.println("Program finished");
        }
    }
}

题二:

package Try;

import java.io.IOException;

public class Test2 {

    public static void main(String[] args) {
        methodB();
    }

    public static void methodA() throws IOException {
        System.out.println("in method A");
    }

    public static void methodB() {
        try {
            methodA();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("in method B");
    }

}

题三:

package Try;

public class MyException extends Exception {
    public MyException() {
    }

    public MyException(String message) {
        super(message);
    }

    public void output() {
        System.out.println(getMessage());
    }

    public static void main(String[] args) {

        try {
            throw new MyException("My Exception.");
        } catch (MyException e) {
            e.output();
            System.out.println(e.getMessage());// e
            e.printStackTrace();
        }
    }
}

题四:

package Try;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test4 {
    public static class Test {
        public static void main(String args[]) {
            Scanner sc = new Scanner(System.in);
            System.out.println("input a radius:");
            try {
                double radius = sc.nextDouble();
                double area = Math.PI * radius * radius;
                System.out.printf("area =%.2f%n", area);
            } catch (InputMismatchException e) {
                System.out.println(e);
                System.out.println("Number Format Error.");
            }
        }
    }
}

源代码:


本站由以下主机服务商提供服务支持:

0条评论

发表评论