1.异常体系结构图
![异常体系结构图](https://i-blog.csdnimg.cn/blog_migrate/e8b56f1a3f3d8cdace0dbbb24fafb8a3.png)
2.StackOverFlowError栈溢出
@WebServlet("/stackOver")
public class StackOverflowServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost.....");
}
}
![错误截图](https://i-blog.csdnimg.cn/blog_migrate/79150f6d1ee6e3f4a838863630a1df79.png)
3.SqlException sql语句异常
public class SqlExceptionTest {
public static void main(String[] args) {
Connection conn = JDBCUtils.getConn();
String sql = "select * form sys_user";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
} catch (SQLException sqlException) {
System.out.println("将异常信息输出到控制台:");
sqlException.printStackTrace();
}
}
}
![异常截图](https://i-blog.csdnimg.cn/blog_migrate/58cbcc77b5787e33913d10dcd42d443f.png)
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());
}
}
![异常截图](https://i-blog.csdnimg.cn/blog_migrate/577e78bbb9b22cf8248a3eee73990936.png)
5.NullPointerException空指针异常
public class NullPointerExceptionTetst {
public static void main(String[] args) {
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);
}
}
![异常截图](https://i-blog.csdnimg.cn/blog_migrate/7f9d53847cfe409e7b000e79aafcdb5b.png)
6.IllegalArgument非法参数异常
public class IllegalArgumentTest {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>(-1);
}
}
![异常截图](https://i-blog.csdnimg.cn/blog_migrate/b53f86e5e092628625f9d07d69a0096f.png)
7.ClassCast类转换异常
public class ClassCastExceptionTest {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Set<String> set = (Set<String>) arrayList;
}
}
![异常截图](https://i-blog.csdnimg.cn/blog_migrate/114b845569beedd98ef41f934292e80e.png)