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

    1.建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。

    package ersan;
    
    public class TestException {
    
        public static void main(String[] args) {
            for (int i = 0; i < 4; i++) {
                int k;
                switch (i) {
                case 0:
                    int zero = 0;
                    try {
                        k = 911 / zero;
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println("数据异常");
                    }
                    break;
                case 1:
                    int b[] = null;
                    try {
                        k = b[0];
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println("空指针异常");
                    }
    
                    break;
                case 2:
                    int c[] = new int[2];
                    try {
                        k = c[9];
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println("数组超出长度");
                    }
    
                    break;
                case 3:
                    try {    char ch = "abc".charAt(99);
                        
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println("类型转换出错");
                    }
            
                    break;
                }
            }
        }
    }
  • 相关阅读:
    子查询
    关联,分组练习
    共享锁(S锁)和排它锁(X锁)
    mac 搭建Vue开发环境
    移动端web开发
    负margin
    关于前端的margin
    清除的通用样式 css
    css布局
    <div class="clear"></div>
  • 原文地址:https://www.cnblogs.com/dandan1224/p/5916428.html
Copyright © 2011-2022 走看看