zoukankan      html  css  js  c++  java
  • java_异常处理

    cmd命令行下编译并执行,不过要将package javase8注释掉,否则会报错

    D:codeworkspacemyjavasrcjavase8>java Exception20171231
    错误: 找不到或无法加载主类 Exception20171231

    在命令行下传递参数,divide,null,test,hh

    //package javase8;
    
    class TestException extends Exception {
        TestException(){super();}
        TestException(String s){super(s);}
    }
    
    class Exception20171231 {
    
        public static void main(String[] args) {
            for (String arg : args) {
                try {
                    thrower(arg);
                    System.out.println("Test "" + arg +"" did't throw an exception");
                } catch (Exception e) {
                    System.out.println("Test "" + arg +"" threw a " + e.getClass() + "
        with message: " + e.getMessage());
                }
            }
        }
    
        static int thrower(String s) throws TestException{
            try {
                if (s.equals("divide")) {
                    int i=0;
                    return i/i;
                }
                if (s.equals("null")) {
                    s=null;
                    return s.length();
                }
                if (s.equals("test")) {
                    throw new TestException("Test Message");
                }
                return 0;
            } finally {
                System.out.println("[thrower("" + s +"") done]");
            }
        }
    }

    D:codeworkspacemyjavasrcjavase8>javac Exception20171231.java

    D:codeworkspacemyjavasrcjavase8>java Exception20171231

    D:codeworkspacemyjavasrcjavase8>java Exception20171231 divide
    [thrower("divide") done]
    Test "divide" threw a class java.lang.ArithmeticException
            with message: / by zero

    D:codeworkspacemyjavasrcjavase8>java Exception20171231 null
    [thrower("null") done]
    Test "null" threw a class java.lang.NullPointerException
            with message: null

    D:codeworkspacemyjavasrcjavase8>java Exception20171231 test
    [thrower("test") done]
    Test "test" threw a class TestException
            with message: Test Message

    D:codeworkspacemyjavasrcjavase8>java Exception20171231 hh
    [thrower("hh") done]
    Test "hh" did't throw an exception

    D:codeworkspacemyjavasrcjavase8>

  • 相关阅读:
    C#下载文件代码更新20070920
    判断IP地址是否合法的sql2000使用存储过程跟函数
    百万级SQL分页存储过程
    C#批量重命名文件代码的实现
    单个分页的存储过程
    阿江网站访问统计系统设计构思分析
    c#查询QQ状态是否在线查询代码
    C# public class Person
    C#禁止应用程序的多重运行
    Outlook最小化到托盘的设置方法
  • 原文地址:https://www.cnblogs.com/createyuan/p/8157671.html
Copyright © 2011-2022 走看看