Exception

Throwable 类是 Java 语言中所有错误或异常的超类

当程序出现一些问题时,可以是严重问题,可以是一种异常

Throwable

  error

Exception

error:可抗力因素, 属于严重问题   例如内存溢出了 (加载页面中大量图片的时候,程序会挂掉---使用第三方的框架去处理:ImageLoader)

Exception:

   1>编译时期异常:只要出现的不是运行时期异常,统称为编译时期  例如:日期的文本格式---解析   java.util.Date类型:ParseException:解析异常

 注意:编译时期异常:开发者必须处理!

2>运行时期异常:RuntimeException,编译可以通过,由于开发者代码不严谨导致的(NullPointerExceptino等等...)

异常的处理分为两种:

  1)try...catch...finally (标准格式) :捕获异常

  2)throws ... 抛出异常

一:try....catch..... 

变形格式...

 try...catch...

 try...catch...catch...

 catch....

1>若只有一个异常的处理格式: 

try{

可能出现问题的代码

   }catch(异常类 异常类对象){

  如果try中的代码出现的异常,常刚好就是catch的异常类,就会执行catch里面代码

  //处理异常代码

 }

 2>若多个异常的处理格式:

   try{

   可能出现问题的代码

  

   }catch(异常类名1 对象名1){

   //异常处理

   }catch(异常类名2 对象名2(){

   //异常处理

  }

 注意: 1>try里面的代码越少越好

2>catch中必须要有内容,哪怕是一个提示,否则没有意义

3>try中有多个异常,只执行一个 就结束try...catch语句

Jdk7以后出现另一种方式处理多个异常:

  try{

   可能出现问题的代码;

  }catch(异常类名1 | 异常类名2 |... 对象名){

   处理异常

  }

编译时期异常和运行时期异常的区别?

  编译时期:开发者必须进行显示处理,不处理,编译不能通过,

运行时期异常:无需进行显示处理,可以像编译时期一样进行处理

如果catch里面有return 语句,finally中的代码还会执行,是在return语句之前执行还是return后执行?

 finally中代码是一定会执行的,是在return

代码:

public class FinallyTest {

public static void main(String[] args) {

System.out.println(getInt());

}

private static int getInt() {

int a ;

try {

a = 10 ;

System.out.println(a/0);

a = 20 ;

}catch(Exception e) {

a= 30 ;

return a ;

/**

 * try的代码出现问题了,执行catch中的语句,30赋值a,

 * return 30(已经形成了一个回路径)finally代码一定会执行(除非Jvm) a = 40 ,在fianlly外面

 * 有return a: a记录回路径的那个a,返回30

 */

}finally {

//除非jvm退出了

a = 40 ;

}

return a;//30

}

}

 

二:使用throws 抛出异常 (跟在方法的后面)

格式:

  xxx 返回值  方法名() throws  异常类名{

  }

Throwable里面的方法

public String getMessage() :消息字符串

public String toString(): 异常的简短描述 ": 由冒号和空格组成

 public void printStackTrace():返回值void 直接调用, 包含了消息字符串,还有": "

信息描述,具体出现异常的代码定位以及定位的源码上

面试题:final,finally,finalize的区别?

final 可以修饰类,该类不能被继承;可以修饰变量,该变量是常量;可以修饰成员方法,该方法重写

finalize:gc():运行垃圾回收器,实际是调用finalize()方法,和垃圾回收器有关系

finally:io,数据库中以及后面对数据库操作(DBUtuls/c3p0/Hibernate/MyBatis)里面中释放资源的

throw:表示也是抛出异常,抛出的一个异常对象 (throw new 异常类名() :匿名对象的方式)

面试题:

throwsthrow的区别?

throws:

表示抛出异常,它后面跟的异常类名,

并且可以多个异常类名中间逗号开

在方法上抛出,由调用者处理

  它表示抛出异常的可能性

 

举例:

 public void show() throws IoException,ClassNotFoundException{...}

throw:

抛出的一个异常对象

  在语句体中抛出的,由语句体进行处理

  它表示抛出异常的肯定性

自定义异常类,两种方式

  1)自定义一个类,这个继承自Exception

  2)继承RuntimeException

异常中的注意事项:

子类继承父类的时候的注意事项

  1)子类重写父类的方法的时候,子类的方法的抛出的异常必须要么是父类的方法异常一样,要么是父类方法异常的子类

  2)子类重写父类方法的时候,如果父类中的这个方法没有抛出异常,那么子类重写的这个方法也不能抛出异常,只能try...catch

 

 

 

阅读更多

Commit exception overridden by rollback exception

06-24

2009-06-24 21:26:51,021 ERROR [HibernateTransactionManager] (AbstractPlatformTransactionManager.java:882) Commit exception overridden by rollback exceptionrnorg.springframework.orm.hibernate3.HibernateSystemException: Session is closed; nested exception is org.hibernate.SessionException: Session is closedrn at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:674)rn at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)rn at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)rn at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:731)rn at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:700)rn at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:322)rn at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)rn at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)rn at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)rn at $Proxy6.getRoomInfo(Unknown Source)rn at edu.njupt.scs.ui.acquireform.AcquireFormAction.retriveTitleinfo(AcquireFormAction.java:71)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn at java.lang.reflect.Method.invoke(Unknown Source)rn at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)rn at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)rn at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)rn at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)rn at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)rn at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)rn at java.lang.Thread.run(Unknown Source)rnCaused by: org.hibernate.SessionException: Session is closedrn at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:139)rn at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170)rn at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)rn at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)rn ... 74 morern

没有更多推荐了,返回首页