java异常处理例题代码教学文稿
- 格式:docx
- 大小:11.63 KB
- 文档页数:5
Java异常(Exception)处理以及常见异常总结⽬录前⾔异常简介异常类型总结前⾔很多事件并⾮总是按照⼈们⾃⼰设计意愿顺利发展的,经常出现这样那样的异常情况。
例如:你计划周末郊游,计划从家⾥出发→到达⽬的→游泳→烧烤→回家。
但天有不测风云,当你准备烧烤时候突然天降⼤⾬,只能终⽌郊游提前回家。
“天降⼤⾬”是⼀种异常情况,你的计划应该考虑到这样的情况,并且应该有处理这种异常的预案。
计算机程序的编写也需要考虑处理这些异常情况。
异常(exception)是在运⾏程序时产⽣的⼀种异常情况,已经成为了衡量⼀门语⾔是否成熟的标准之⼀。
⽬前的主流编程语⾔java也提供了异常处理机制。
异常简介Java中的异常⼜称为例外,是⼀个在程序执⾏期间发⽣的事件,它中断正在执⾏程序的正常指令流。
为了能够及时有效地处理程序中的运⾏错误,必须使⽤异常类,这可以让程序具有极好的容错性且更加健壮。
在 Java 中⼀个异常的产⽣,主要有如下三种原因:1. Java 内部错误发⽣异常,Java 虚拟机产⽣的异常。
2. 编写的程序代码中的错误所产⽣的异常,例如空指针异常、数组越界异常等。
3. 通过 throw 语句⼿动⽣成的异常,⼀般⽤来告知该⽅法的调⽤者⼀些必要信息。
Java 通过⾯向对象的⽅法来处理异常。
在⼀个⽅法的运⾏过程中,如果发⽣了异常,则这个⽅法会产⽣代表该异常的⼀个对象,并把它交给运⾏时的系统,运⾏时系统寻找相应的代码来处理这⼀异常。
我们把⽣成异常对象,并把它提交给运⾏时系统的过程称为拋出(throw)异常。
运⾏时系统在⽅法的调⽤栈中查找,直到找到能够处理该类型异常的对象,这⼀个过程称为捕获(catch)异常。
例 1为了更好地理解什么是异常,下⾯来看⼀段⾮常简单的 Java 程序。
下⾯的⽰例代码实现了允许⽤户输⼊ 1~3 以内的整数,其他情况提⽰输⼊错误。
package io.renren.config;import java.util.Scanner;/*** Created by LiYangYong*/public class TestException {public static void main(String[] args) {System.out.println("请输⼊您的选择:(1~3 之间的整数)");Scanner input = new Scanner(System.in);int num = input.nextInt();switch (num) {case 1:System.out.println("one");break;case 2:System.out.println("two");break;case 3:System.out.println("three");break;default:System.out.println("error");break;}}}正常情况下,⽤户会按照系统的提⽰输⼊ 1~3 之间的数字。
实验十一Java中的异常处理x=n.nextInt();if(x!=999)sum+=x;}catch(InputMismatchException e){}System.out.println(sum);}}}class TestXin{/*** @param args*/public static void main(String[] args) { // TODO Auto-generated method stubXin n=new Xin();System.out.println(n);}}结果:五、实验总结(对本实验结果进行分析, 实验心得体会及改进意见)5.分析程序的运行结果(1)class A{A(){System.out.println("A structure!");}A(int m){System.out.println("A("+m+")");}}public class B extends A{B(int m){System.out.println("B("+m+")");}public static void main(String[] args) { // TODO Auto-generated method stubnew B(100);}}(2)class A{A(){System.out.println("A structure!");}A(int m){System.out.println("A("+m+")");}}public class B extends A{B(){System.out.println("B structure!");}B(int m){System.out.println("B("+m+")");}public static void main(String[] args) { // TODO Auto-generated method stubnew B(100);}}(3)class A{A(){System.out.println("A structure!");}A(int m){System.out.println("A("+m+")");}}public class B extends A{B(){System.out.println("B structure!");}B(int m){//super(m);System.out.println("B("+m+")");}public static void main(String[] args) { new B(100);}}(4)class A{A(){System.out.println("A structure!");}A(int m){System.out.println("A("+m+")");}}public class B extends A{B(int m){super(m);//System.out.println("B("+m+")");}public static void main(String[] args) { new B(100);} }。
程序异常处理引子O在调试航班体息录入程冲•时.町能会遇到这歼的情爲:应用程斤运行过程中.突然中止,屛棗上出现一丸堆英丈让人不知所培・o崔许多域市.很行ATM机随处可见,取歇非常方使•崔ATM机上取欲必须扶輒一定钓步裝携作.若挨作出会相应的災示传息.指导你下一步地楼作.比如密码输入错谖.ATM机将会显示诗码输入错谋・请重新输入”的消息•如是三次密碣伦入梆有谋.則呑卡没商量.o尢论什么斥因引起的程斤运行不正常.都认为是悝序出现了并常.知识要点。
异常的概念。
常见的异常情况。
异常处理的机制◎利用try catch finally实现异常处理o利用thro灵throws创建用户自定义异常知识学习一异常概念。
异常一血巾把程序运行中可能遇到的错罠分为两类.一类是非致命的.的过某种修正后程号还能键续执行.这类捲谖称为异常.边是一类特殊的运行错误対象. 另一类是致命的.即程序遇到了非常严重的不正常状态,不能简单地恢复轨行,这就是钳渓.叩异常类基类throwablcift生出两个子类:Error类:定义了Java程序运行时出现了灾难性失败的畀常.一股来说java程序不捕获也不拋出^Error的对象(包把动态磁接失败.虛拟机错逞等)•而只处理Except ion类的各子类对Exception类:定义了程序可以捕捉到的异常。
异営分两类:1)条统定义的2)用户定义的;・:・异常处理一对于异常,Java使用一种错误捕获方法进行) 处理,称为异常处理. 1斗处理异常的两种方式-是将异常交田Jam异常处理机制的预设处理方法来处理。
二是利用Java提供的try-cat ch-final ly语句对于㊁能出现的异常作预先处理。
"融辜习一程序运行的错误类型及位置识学习程序ArithmeticException 算术异常整数除法中,如果除数为0,则发生该类井常.Java虛拟机遇到这样的错i昊时会中止程序的执行流程,并新建~个ArithmeticException类的对象.即拋出一个算术运算异常.例如下面的程序将引发ArithmeticException异常:public class Test ArithmeticExceptionfpublic static void main(String args[])(int denominator=0,numerator=20jesult=0;resultanumerator/denominator;〃除数为0,将引发ArithmeticException异席System.out.prlntln(result);知识学习一程序运行的错误类型及位置NullPointerExcept ion异常发生的原因.通常是由于应用程序企图在某个需要的对鮫上使用mil I值。
java异常解决方案一、Hibernate(1)org.hibernate.TransientObjectException: object references an unsaved transient instance....(2)org.springframework.orm.hibernate3.HibernateSystemException: Don't change the reference to a collection withcascade="all-delete-orphan": entity.Klass.students; nested exception is org.hibernate.HibernateException: Don't change the reference to a collection with cascade="all-delete-orphan": entity.Klass.students二、Tomcat(1)tomcat启动时报错:java.io.EOFException(2)tomcat内存溢出三、JAVA基本(1)ng.ClassCastException:(2)ng.UnsupportedClassVersionError: Bad version number in .class file四、JSP(1)javax.servlet.jsp.JspException:(2)org.apache.jasper.JasperException: Unable to compile class for JSP:(3)Servlet.service() for servlet jsp threw exceptionng.Error: Unresolved compilation problem:(4)ng.Error: Unresolved compilation problem:The method contextInitialized(ServletContextEvent) of type CreateDataSourceTableListener must override a superclass method(5)Servlet.service() for servlet jsp threw exception ng.Error: Unresolved compilation problem:The method setCharacterEncoding(String) is undefined for the type ServletResponse五、SSH整合(1)ng.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener(2)Exception starting filter struts2 Class:com.opensymphony.xwork2.spring.SpringObjectFactory File: SpringObjectFactory.java Method: getClassInstance(3)(7)(8)org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'biz' defined in class path... (4)Exception starting filter struts2The action name cannot be the same as the action suffix [Action] - Class: org.apache.struts2.convention.SEOActionNameBuilder (5)avax.management.RuntimeErrorException: Error thrown in preDeregister methodCaused by: ng.NoClassDefFoundError:org/apache/struts2/util/ObjectFactoryDestroyable(6)Unable to load configuration. - bean -jar:file:/D:/Tomcat/tomcat/apache-tomcat-6.0.30/webapps/tes t/WEB-INF/lib/struts2-core-2.2.3.1.jar!/struts-default.xml: 29:72六、Struts(1)ng.NoSuchMethodException:action.StudentAction.getAllStudent()(2)Caused by: ng.ClassCastException:org.apache.xerces.parsers.XML11Configuration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration (3)No result defined for action and result七、Android(1)There is no android project named 'cm-android'一、Hibernate一(1)org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: er某个对象的某个属性是一个实体,在这个实体没有保存之前就保存这个对象而造成了这个错误。