异常处理思维指导
程序员文章站
2022-04-17 11:42:13
...
首先我们来看一个异常,异常一般分为三个部分:报的什么异常,异常大致信息,异常归根于那个地方,举个例子:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.anzhuo.cm.teachersDemo.Juxing.jisuanmianji(Juxing.java:10)
at com.anzhuo.cm.teachersDemo.Juxing.main(Juxing.java:5)
以上是个很简单的报错信息,首先我们先看第一行冒号以前的看看到底报的是什么类型的异常,这是报的一个算术异常,再看冒号右边的看看大致是报的什么信息,这里说是除通过零,这个地方问题已经很明确了,它说是报的算术异常,并且除数是0,那么除数能为0吗,不能的话那么再看下面归根于哪个地方,第一个地方是juxing类的第10行,第二个地方是juxing类的第5行,来我们看下代码:
1. package com.anzhuo.cm.teachersDemo;
2.
3. public class Juxing {
4. public static void main(String[] args) {
5. Juxing.jisuanmianji(5, 0);
6. 7. }
8.
9. public static int jisuanmianji(int i,int j){
10. int f = i/j;
11. System.out.println(f);
12. if(f%2==0){
13.System.out.println("偶数");
14. }else{
15. System.out.println("奇数");
16. }
17. return f;
18. }
19.}
我们再来看个异常,请看 ↓
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'Users'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.anzhuo.bean.User
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:35)
at com.anzhuo.test.Test.main(Test.java:16)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'Users'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.anzhuo.bean.User
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:106)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:89)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:49)
... 2 more
Caused by: org.apache.ibatis.builder.BuilderException: Error registering typeAlias for 'Users'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.anzhuo.bean.User
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:127)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:96)
... 4 more
Caused by: java.lang.ClassNotFoundException: Cannot find class: com.anzhuo.bean.User
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:188)
at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:87)
at org.apache.ibatis.io.Resources.classForName(Resources.java:254)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:120)
... 5 more
这个报错看着比较多,先别慌,首先我们先看第一行冒号以前的看看到底报的是什么类型的异常,假如看不懂就跳过,看冒号后面的建筑数据库会话错误,该错误可能存在于SQL Mapper配置中,看完这了再看归根于那个地方,这样以此类推就出来了,看caused 或caused by,这个后面都讲的很清楚,解析SQL Mapper配置时出错,注册’Users’的类型别名时出错。无法找到com.anzhuo.bean.User类;这样一来;我们在看下代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "config" "mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<typeAlias alias="Users" type="com.anzhuo.bean.User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatisdemo"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/anzhuo/dao/mapper.xml"/>
</mappers>
</configuration>
以上看别名的地方找不到类,因为我的实体类是Users,所以把实体类的路径改正就好了,真确跟着之前和之后对比:
之前:
<typeAliases>
<typeAlias alias="Users" type="com.anzhuo.bean.User"/>
</typeAliases>
之后:
<typeAliases>
<typeAlias alias="Users" type="com.anzhuo.bean.Users"/>
</typeAliases>
用了我这个方法是不是很简单呀