SqlException,IOException,ClassCastException,NullPointerException,IllegalArgumentException常用异常及解决方案

1.异常体系结构图

异常体系结构图

2.StackOverFlowError栈溢出

@WebServlet("/stackOver")
public class StackOverflowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //会报栈溢出StackOverflowError
        this.doGet(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost.....");
    }
}

错误截图

3.SqlException sql语句异常

public class SqlExceptionTest {
    public static void main(String[] args) {
        Connection conn = JDBCUtils.getConn();
        //sql语句错误
        String sql = "select * form sys_user";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            //执行sql语句
            ResultSet rs = ps.executeQuery();
        } catch (SQLException sqlException) { //java.sql.SQLSyntaxErrorException
            //将异常信息输出到控制台
            System.out.println("将异常信息输出到控制台:");
            sqlException.printStackTrace();
        }
    }
}

异常截图

4.IOException 文件上传时发生异常

@WebServlet("/ioException")
@MultipartConfig
public class IOExceptionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part pic = req.getPart("pic");
        String realPath = this.getServletContext().getRealPath("/pic/");
        //一个不存在的路径
        pic.write(realPath+pic.getSubmittedFileName());
    }
}

异常截图

5.NullPointerException空指针异常

public class NullPointerExceptionTetst {
    public static void main(String[] args) {
        //list类、接口
        //add();类(接口)中的方法

        //1.设计类  2.实例化对象(new+构造器())  3.打点调用方法
        //少了第2步就会报空指针异常

        List<String> list= null;
        try{
            //异常处理就是让异常不影响其它代码的执行
            list.add("a");
            System.out.println("我不会打印");
        } catch (NullPointerException e){
            e.printStackTrace();
        }
        //会不会执行 ,异常处理后依旧会执行下面的代码
        list = new ArrayList<>();
        list.add("b");
        System.out.println(list);
        String str = " ";
        int length = str.length();
        System.out.println(length);
    }
}

异常截图

6.IllegalArgument非法参数异常

public class IllegalArgumentTest {
    public static void main(String[] args) {
        //()参数:初始容量:集合的底层:是数组
        ArrayList<String> arrayList = new ArrayList<>(-1);
    }
}

异常截图

7.ClassCast类转换异常

public class ClassCastExceptionTest {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        //将arrayList强转成set,会报错
        Set<String> set = (Set<String>) arrayList;
    }
}

异常截图

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值