@liayun
2016-05-25T15:56:25.000000Z
字数 9276
阅读 1684
java基础
异常:就是程序在运行时出现的不正常情况。
异常的由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象。异常其实就是java对不正常情况进行描述后的对象体现。
对于问题的划分有两种:一种是严重的问题,一种是非严重的问题。
Error类进行描述(通常出现重大问题如:运行的类不存在或者内存溢出等)。对于Error一般不编写针对性的代码对其进行处理。Exception类进行描述。对于Exception可以使用针对性的处理方式进行处理。无论Error或者Exception都具有一些共性内容,比如:不正常情况的信息,引发原因等。
java提供了特有的语句进行处理:
try {需要被检测的代码;} catch(异常类 变量) {处理异常的代码;(处理方式)} finally {一定会执行的语句;}
例,
class Demo {int div(int a, int b) throws Exception { // 在功能上通过throws的关键字声明了该功能有可能会出现问题return a/b; // new ArithmeticException();}}class ExceptionDemo {public static void main(String[] args) {Demo d = new Demo();try {int x = d.div(4, 1); // new ArithmeticException();System.out.println("x="+x);} catch(Exception e) { // Exception e = new ArithmeticException();System.out.println("除零啦!");System.out.println(e.getMessage()); // / by zeroSystem.out.println(e.toString());// 异常名称:异常信息e.printStackTrace(); // 异常名称,异常信息,异常出现的位置// 其实JVM默认的异常处理机制,就是在调用printStackTrace(),打印异常在堆栈中的跟踪信息}System.out.println("over");}}
在函数上声明异常(throws)。便于提高安全性,让调用者进行处理,不处理编译失败。
建议在进行catch处理时,catch中一定要定义具体处理方式。不要简单定义一句:e.printStackTrace();,也不要简单的就书写一条输出语句。
例,
class Demo {int div(int a, int b) throws ArithmeticException, ArrayIndexOutOfBoundsException { // 在功能上通过throws的关键字声明了该功能有可能会出现问题int[] arr = new int[a];System.out.println(arr[4]);return a/b; // new ArithmeticException();}}class ExceptionDemo {public static void main(String[] args) {Demo d = new Demo();try {int x = d.div(5, 0); // new ArithmeticException();System.out.println("x="+x);} catch(ArithmeticException e) {System.out.println(e.toString());System.out.println("除零啦!");} catch(ArrayIndexOutOfBoundsException e) {System.out.println(e.toString());System.out.println("角标越界啦!");} catch(Exception e) {System.out.println("haha:"+e.toString());}System.out.println("over");}}
因为项目中会出现特有的问题,而这些问题并未被java所描述并封装对象。 所以对于这些特有的问题可以按照java的对问题封装的思想,将特有的问题,进行自定义的异常封装。
当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。要么在内部try catch处理,要么在函数上声明让调用者处理。一般情况下,函数内出现异常,函数上需要声明。
自定义异常如何定义异常信息?
答:因为父类中已经把异常信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类,通过super语句,那么就可以直接通过getMessage()获取自定义的异常信息。
自定义异常,必须是自定义类继承Exception。继承Exception的原因:异常体系有一个特点,因为异常类和异常对象都被抛出,它们都具备可抛性,这个可抛性是Throwable这个体系中独有的特点,只有这个体系中的类和对象才可以被throws和throw操作。
throws和throw的区别:
throws使用在函数上,throws后面跟的是异常类,可以跟多个,用逗号隔开。throw使用在函数内,throw后跟的是异常对象。例,
/*需求:在本程序中,对于除数是-1,也视为是错误的,是无法进行运算的,那么就需要对这个问题进行自定义的描述*/class FuShuException extends Exception {private int value;FuShuException() {super();}FuShuException(String msg, int value) {super(msg);this.value = value;}public int getValue() {return value;}}class Demo {int div(int a, int b) throws FuShuException {if(b < 0)throw new FuShuException("出现除数是负数的情况----- / by fushu", b); // 手动通过throw关键字抛出一个自定义异常对象return a/b;}}class ExceptionDemo2 {public static void main(String[] args) {Demo d = new Demo();try {int x = d.div(4, -9);System.out.println("x="+x);} catch(FuShuException e) {System.out.println(e.toString());// System.out.println("除数出现负数了");System.out.println("错误的负数是:"+e.getValue());}System.out.println("over");}}
Exception中有一个特殊的子类异常RuntimeException(运行时异常)。
自定义异常时:如果该异常的发生,无法再继续进行运算,就让自定义异常继承RuntimeException。
对于异常分两种:
RuntimeException以及其子类)例,
class FuShuException extends RuntimeException {FuShuException(String msg) {super(msg);}}class Demo {int div(int a, int b) {if(b < 0)throw new FuShuException("出现了除数为负数啦!");if(b == 0)throw new ArithmeticException("被零除啦!");return a/b;}}class ExceptionDemo3 {public static void main(String[] args) {Demo d = new Demo();int x = d.div(4, -9);System.out.println("x="+x);System.out.println("over");}}
练习:毕老师用电脑上课。
解:
分析:开始思考上课中出现的问题。比如问题是:电脑蓝屏、电脑冒烟。要对问题进行描述,封装成对象。可是当冒烟发生后,出现讲课进度无法继续,出现了讲师的问题,课时计划无法完成。
class LanPingException extends Exception {LanPingException(String message) {super(message);}}class MaoYanException extends Exception {MaoYanException(String message) {super(message);}}class NoPlanException extends Exception {NoPlanException(String msg) {super(msg);}}class Computer {private int state = 3;public void run() throws LanPingException, MaoYanException {if(state == 2)throw new LanPingException("蓝屏了");if(state == 3)throw new MaoYanException("冒烟了");System.out.println("电脑运行");}public void reset() {state = 1;System.out.println("电脑重启");}}class Teacher {private String name;private Computer cmpt;Teacher(String name) {this.name = name;cmpt = new Computer();}public void prelect() throws NoPlanException {try {cmpt.run();} catch(LanPingException e) {cmpt.reset();} catch(MaoYanException e) {test();throw new NoPlanException("课时无法继续----"+e.getMessage());}System.out.println("讲课");}public void test() {System.out.println("练习");}}class ExceptionTest {public static void main(String[] args) {Teacher t = new Teacher("毕老师");try {t.prelect();} catch(NoPlanException e) {System.out.println(e.toString());System.out.println("换老师,或者放假");}}}
定义一定执行的代码。通常用于关闭资源。
finally使用举例:
例1,
class FuShuException extends Exception {FuShuException(String msg) {super(msg);}}class Demo {int div(int a, int b) throws FuShuException {if(b < 0)throw new FuShuException("出现了除数为负");return a/b;}}class ExceptionDemo {public static void main(String[] args) {Demo d = new Demo();try {int x = d.div(4, -1);System.out.println("x="+x);} catch (FuShuException e) {System.out.println(e.toString());return;} finally {System.out.println("finally"); // finally中存放的是一定会被执行的代码}System.out.println("over");}}
例2,操作数据库
class NoException extends Exception {}public void method() throws NoException {连接数据库;数据操作; //throw new SQLException();关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源。try {连接数据库;数据操作; //throw new SQLException();} catch(SQLException e) {会对数据库进行异常处理;throw new NoException();} finally {关闭数据库;}}
记住一点:catch是用于处理异常,如果没有catch就代表异常没有被处理过,如果该异常是检测时异常,那么必须声明。
try处理,绝对不能抛。练习:有一个圆形和长方形,它们都可以获取面积,对于面积如果出现非法的数值,视为是获取面积出现问题。问题通过异常来表示。
解:先要对这个程序进行基本设计。
class NoValueException extends RuntimeException {NoValueException(String message) {super(message);}}interface Shape {void getArea();}class Rec implements Shape {private int len, wid;Rec(int len, int wid) {if(len <= 0 || wid <= 0)throw new NoValueException("出现非法值");this.len = len;this.wid = wid;}public void getArea() {System.out.println(len*wid);}}class Circle implements Shape {private int radius;public static final double PI = 3.14;Circle(int radius) {if(radius <= 0)throw new NoValueException("非法值");this.radius = radius;}public void getArea() {System.out.println(radius*radius*PI);}}class ExceptionTest {public static void main(String[] args) {Rec r = new Rec(3, 4);r.getArea();Circle c = new Circle(-8);System.out.println("over");}}
异常:对问题的描述,将问题进行对象的封装。
异常体系:
Throwable
|---Error
|---Exception
|---RuntimeException
异常体系的特点:异常体系中的所有类以及建立的对象都具有可抛性,也就是说可以被throw和throws关键字所操作。只有异常体系具备这个特点。
throw和throws的用法:
当函数内容有throw抛出异常对象,并未进行try处理,必须要在函数上声明,否则编译失败。
注意:RuntimeException除外,也就是说,函数内如果抛出的是RuntimeException异常,函数上可以不用声明。
如果函数声明了异常,调用者需要进行处理,处理方式可以throws也可以try。
异常有两种:
try),编译失败异常处理语句:
try {需要被检测的代码;} catch() {处理异常的代码;} finally {一定会指执行的代码;}
有三种结合格式:
try {需要被检测的代码;} catch() {处理异常的代码;}
try {需要被检测的代码;} finally {一定会指执行的代码;}
try {需要被检测的代码;} catch() {处理异常的代码;} finally {一定会指执行的代码;}
注意:
finally中定义的通常是关闭资源代码,因为资源必须释放。finally只有一种情况不会执行,当执行到System.exit(0);, finally不会被执行。自定义异常:定义类继承Exception或者RuntimeException。
当要定义自定义异常的信息时,可以使用父类已经定义好的功能。将异常信息传递给父类的构造函数。
class MyException extends Exception {MyException(String message) {super(message);}}
自定义异常:按照java的面向对象思想,将程序出现的特有问题进行封装。
异常的好处:
异常的处理原则:
try或者throwstry对应多个catchcatch,父类的catch放到最下面catch内,需要定义针对性的处理方式,不要简单的定义printStackTrace或者输出语句。也不要不写当捕获到的异常,本功能处理不了时,可以继续在catch中抛出。
try{throw new AException();} catch(AException e) {throw e;}
如果该异常处理不了,但并不属于该功能出现的异常。可以将异常转换后,在抛出和该功能相关的异常。
try{throw new AException();} catch(AException e) {throw new BException();}
或者异常可以处理,但需要将异常产生后,和本功能相关的问题提供出去,让调用者知道并处理,即将捕获异常处理后,转换新的异常抛出。(比如,汇款的例子)
try{throw new AException();} catch(AException e) {对AException处理throw new BException();}
异常的注意事项:
在子父类覆盖时
包名.类名总结:
public修饰。protected权限修饰的成员。public/protected| ┈ | public | protected | default | private |
|---|---|---|---|---|
| 同一类中 | √ | √ | √ | √ |
| 同一包中 | √ | √ | √ | |
| 子类 | √ | √ | ||
| 不同包中 | √ |
为了简化类名的书写,使用一个关键字——import。
import导入的是包中的类,不导入包中的包。
建议:
*,需要用到包中的哪个类,就导入哪个类url来完成定义,url是唯一的例,
package pack;import packb.haha.hehe.heihei.*;import packa.*;class PackageDemo {public static void main(String[] args) {DemoC c = new DemoC();}}
javac -d . PackageDemo.java,.指定的是当前目录,如果编译成功,会在当前目录下生成一个pack的文件夹,里面是编译的class文件。当然了也可以指定其他你想指定的任何目录,如:c:\myclass。java pack.PackageDemo。Java的压缩包
classpath设置jar路径即可jar包体现的Jar包的操作
通过jar.exe工具对jar的操作。
jar -cvf mypack.jar packa packbjar -tvf mypack.jar [>定向文件]jar -xvf mypack.jarjar –cvfm mypack.jar mf.txt packa packbjar包中的内容很多,在dos命令行环境中一屏显示不过来,可用如下命令:
C:\myclass>jar -tf yelei.jar >c:\1.txt
即将yelei.jar包中的内容存放到c盘下1.txt文本文档中。
数据重定向——数据不想在一个地方(例如dos命令行)显示,而想在文件中显示。可用如下命令:
C:\>dir >c:\2.txt
即将c盘目录下的文件信息存放在c盘下2.txt文本文档中。