zoukankan      html  css  js  c++  java
  • java课堂小练习9

    import javax.swing.*;

    class AboutException {
    public static void main(String[] a)
    {
    int i=1, j=0, k;
    k=i/j;


    try
    {

    k = i/j; // Causes division-by-zero exception
    //throw new Exception("Hello.Exception!");
    }

    catch ( ArithmeticException e)
    {
    System.out.println("被0除. "+ e.getMessage());
    }

    catch (Exception e)
    {
    if (e instanceof ArithmeticException)
    System.out.println("被0除");
    else
    {
    System.out.println(e.getMessage());

    }
    }


    finally
    {
    JOptionPane.showConfirmDialog(null,"OK");
    }

    }
    }

    结果:

    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at about.main(about.java:6)

    public class CatchWho {
    public static void main(String[] args) {
    try {
    try {
    throw new ArrayIndexOutOfBoundsException();
    }
    catch(ArrayIndexOutOfBoundsException e) {
    System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
    }

    throw new ArithmeticException();
    }
    catch(ArithmeticException e) {
    System.out.println("发生ArithmeticException");
    }
    catch(ArrayIndexOutOfBoundsException e) {
    System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
    }
    }

    结果:

    ArrayIndexOutOfBoundsException/外层try-catch


    public class EmbededFinally {


    public static void main(String args[]) {

    int result;

    try {

    System.out.println("in Level 1");


    try {

    System.out.println("in Level 2");
    // result=100/0; //Level 2

    try {

    System.out.println("in Level 3");

    result=100/0; //Level 3

    }

    catch (Exception e) {

    System.out.println("Level 3:" + e.getClass().toString());

    }


    finally {

    System.out.println("In Level 3 finally");

    }


    // result=100/0; //Level 2


    }

    catch (Exception e) {

    System.out.println("Level 2:" + e.getClass().toString());

    }
    finally {

    System.out.println("In Level 2 finally");

    }

    // result = 100 / 0; //level 1

    }

    catch (Exception e) {

    System.out.println("Level 1:" + e.getClass().toString());

    }

    finally {

    . System.out.println("In Level 1 finally");

    }

    }

    }

    结果:



    当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。


    public class SystemExitAndFinally {


    public static void main(String[] args)
    {

    try{


    System.out.println("in main");

    throw new Exception("Exception is thrown in main");

    //System.exit(0);


    }

    catch(Exception e)

    {

    System.out.println(e.getMessage());

    System.exit(0);

    }

    finally

    {

    System.out.println("in finally");

    }

    }


    }

    finally语句块一定会执行吗?

    至少有两种情况下finally语句是不会被执行的:
    (1)try语句没有被执行到,如在try语句之前return就返回了,这样finally语句就不会执行。这也说明了finally语句被执行的必要而非充分条件是:相应的try语句一定被执行到。

    (2)在try块|catch块中有System.exit(0);这样的语句。System.exit(0)是终止java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。

    // UsingExceptions.java
    // Demonstrating the getMessage and printStackTrace
    // methods inherited into all exception classes.
    public class PrintExceptionStack {
    public static void main( String args[] )
    {
    try {
    method1();
    }
    catch ( Exception e ) {
    System.err.println( e.getMessage() + " " );
    e.printStackTrace();
    }
    }

    public static void method1() throws Exception
    {
    method2();
    }

    public static void method2() throws Exception
    {
    method3();
    }

    public static void method3() throws Exception
    {
    throw new Exception( "Exception thrown in method3" );
    }
    }

    如何跟踪异常的传播路径?

    当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。

    可使用printStackTrace 和 getMessage方法了解异常发生的情况: printStackTrace:打印方法调用堆栈。

    每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

    import java.io.*;
    public class ThrowMultiExceptionsDemo {
    public static void main(String[] args)
    {
    try {
    throwsTest();
    }
    catch(IOException e) {
    System.out.println("捕捉异常");
    }
    }

    private static void throwsTest() throws ArithmeticException,IOException {
    System.out.println("这只是一个测试");
    // 程序处理过程假设发生异常
    throw new IOException();
    //throw new ArithmeticException();
    }
    }

    结果:

    这只是一个测试
    捕捉异常

    一个方法可以声明抛出多个异常

    int g(float h) throws OneException,TwoException { …… }

    import java.io.*;


    public class OverrideThrows
    {
    public void test()throws IOException
    {
    FileInputStream fis = new FileInputStream("a.txt");
    }
    }
    class Sub extends OverrideThrows
    {
    //如果test方法声明抛出了比父类方法更大的异常,比如Exception
    //则代码将无法编译……
    public void test() throws FileNotFoundException
    {
    //...
    }
    }

    这只是一个测试
    捕捉异常

    一个子类的throws子句抛出的异常,不能是其基类同名方法抛出的异常对象的父类。

    编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

    要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

    import javax.swing.JOptionPane;
    public class Score {
    public static void main(String[] args){
    for(;;)
    {
    String a = JOptionPane.showInputDialog("请输入一个成绩:");
    try{
    int b = Integer.parseInt(a);
    if(b>0&&b<60){
    JOptionPane.showMessageDialog(null,
    "不及格!");
    break;
    }
    else if(b>=60&&b<=70)
    {
    JOptionPane.showMessageDialog(null,
    "及格!");
    break;
    }
    else if(b>70&&b<=80)
    {
    JOptionPane.showMessageDialog(null,
    "成绩中等!");
    break;
    }
    else if(b>80&&b<=90)
    {
    JOptionPane.showMessageDialog(null,
    "成绩良好!");
    break;
    }
    else if(b>90&&b<=100)
    {
    JOptionPane.showMessageDialog(null,
    "成绩优秀!");
    break;
    }
    else if(b>100||b<0)
    {
    JOptionPane.showMessageDialog(null,
    "您输入的成绩超出范围,请重新输入!");
    }
    }
    catch(Exception e){
    JOptionPane.showMessageDialog(null,
    "您的输入有误,请重新输入!");
    }
    }

    }
    }

  • 相关阅读:
    6.Dump域内用户Hash姿势集合
    4.浅谈跨域劫持
    7. Smali基础语法总结
    7.linux安全基线加固
    12. git常用语法总结
    5.内网渗透之PTH&PTT&PTK
    4. 内网渗透之IPC$入侵
    1.我所了解的内网渗透
    34.不安全的HTTP
    2.内网渗透之端口转发
  • 原文地址:https://www.cnblogs.com/xxdcxy/p/6094834.html
Copyright © 2011-2022 走看看