您的位置:首页 > 编程语言 > Java开发

java个人学习笔记14(Error+Exception+try_catch_finally+throw+throws)

2014-03-17 21:32 816 查看
1.异常:java程序在运行时期发生的不正常情况,java按照面向对象的思想对不正常情况进行描述和对象封装

2.两种问题:

Throwable:定义了对于问题的共性的功能

|--Error:系统低层发生——>jvm——>调用者,不做针对性处理,直接修改代码

|--Exception:jvm发生——>调用者,可以进行针对性处理

3.异常处理:

1)遇到问题不进行具体的处理,而是继续抛给调用者。其实就是在函数上通过throws关键字声明异常,告诉调用者处理,

若一直没有得到针对性的处理,异常最终将会抛给jvm,jvm进行默认处理:在控制台输出错误信息

2)针对性的处理方式:捕获

try{

//有可能发生异常的代码

}

catch(异常类 变量){

//处理异常的代码

}

finally{

只有出现了System.exit(0);//退出jvm,该代码块才不会被执行(即使之前出现return ;也能执行)

//一定会被执行的代码

}

声明异常:

class Math{
public int div(int a,int b) throws Exception//声明异常
{
return a/b;//throw new ArithmeticException("/ by zero"):向上(main)抛出异常
}
}

class ExceptionDemo{
public static void main(String[] args) throws Exception//声明异常
{
Math d = new Math();
int res = d.div(4,0);//throw new ArithmeticException("/ by zero"):向上(jvm)抛出异常,程序运行至此就结束了
System.out.println(res);
System.out.println("Over");
}
}



捕获异常:

class Math{
public int div(int a,int b) throws Exception//声明异常
{
//	if(b==0)
//		throw new ArihmeticException("除数不能为零");//自定义异常信息
return a/b;//throw new ArithmeticException("/ by zero"):向上(main)抛出异常
}
}

class ExceptionDemo{
public static void main(String[] args)
{
Math d = new Math();
try{//有可能发生异常的代码
int res = d.div(4,0);
System.out.println(res);
}
catch(Exception e){//进行针对性处理,div扔出什么样的异常,此处便捕获什么样的异常
//Exception e = new ArithmeticException("/by zero")
System.out.println("ArithmeticException: / by zero");
System.out.println(e.getMessage());//异常信息
System.out.println(e.toString());//异常名称+异常信息
e.printStackTrace();//异常名字+信息+位置,jvm默认处理收到异常的方式
}
System.out.println("Over");//程序将会运行完毕
}
}




4.throw和throws的区别

1)位置不同:

throws用在函数上,后面跟的是异常类,可以跟多个

throw用在函数内,后面跟的是异常对象

2)功能不同:

throws用类声明异常,让调用者知道该功能有可能出现问题,并由调用者给出预先的处理方式

throw抛出具体问题对象,执行到throw该函数就结束,跳转到调用者,并将具体问题对象抛给调用者

也就是说throw语句独立存在时,下面不要写其他语句,执行不到

3)异常体系:体系中的类以及类所产生的对象都具备可抛性,即可以被throw和throws操作

Throwable

|----Error

|----Exception

5.异常抛出原则:

1)功能内部有异常throw抛出,功能上一定throws声明,且内部抛出什么异常,功能上就声明什么异常

2)特殊情况:

当函数内通过throw抛出了RuntimeException及其子类的异常对象时,函数上可以不用throws声明

原因:不声明的目的就是不让调用者处理,让调用者的程序停止,要对代码进行修改

Exception

|----编译时会被检测的异常

|----运行时异常(编译时不会检测)RuntimeException及其子类

5.自定义异常:定义一个除数不能为负数的异常

class FuShuException extends RuntimeException//继承运行时异常,函数内抛出,函数外不声明
{
FuShuException(String message){
super(message);//调用父类已有的方法
}
FuShuException(){
super();//构造函数重载overload
}
}

class Divide{
public int div(int a,int b){
if(b == 0)
throw new ArithmeticException("除数不能为0");
if(b < 0)
throw new FuShuException("除数不能为负数");
return a/b;
}
}

class ExceptionDemo
{
public static void main(String[] args)
{
Divide d = new Divide();
System.out.println(d.div(4,-2));
}
}




6.小结

1)异常:其实就是将问题封装成对象,并抛给调用者。如果函数上声明了,就需要调用者处理(继续声明或捕获)

2)声明或捕获:功能内部可以解决,就捕获;不能解决,或者解决了还必须告诉调用者问题,就该声明

3)一try{}多catch{}

4)异常转换:封装本层异常,对外暴露调用者能处理的异常。

class Computer{
private int state = 2;//computer normal
public void run() throws BlueScreenException,SmokeException{//声明多种异常,多catch来捕获不同的异常
if(state == 1)
throw new BlueScreenException("BlueScreen...");
if(state == 2)
throw new SmokeException("Smoking...");
System.out.println("Running...");
}
public void reset(){
state = 0;
System.out.println("Resetting...");
}
}
//电脑异常:蓝屏
class BlueScreenException extends Exception{
BlueScreenException(){
super();
}
BlueScreenException(String message){
super(message);
}
}
//电脑异常:冒烟
class SmokeException extends Exception{
SmokeException(){
super();
}
SmokeException(String message){
super(message);
}
}
class Teacher{
private String name;
private Computer com;
Teacher(String name){
this.name = name;
com = new Computer();
}
public void lecture() throws StopException{
try{
com.run();
System.out.println("Speaking...");
}
catch(BlueScreenException e){
System.out.println(e.toString());//输出异常信息
com.reset();//电脑重启
lecture();//继续讲课
}
catch(SmokeException e){
System.out.println(e.toString());
throw new StopException("Stopping lecture...");//捕获电脑冒烟异常,向老师抛出讲课进度停止异常
//异常转换:捕获本层异常,对外暴露调用者能处理的异常
}
}//一try多catch
}
//老师异常:讲课进度停止
class StopException extends Exception{
StopException(){
super();
}
StopException(String message){
super(message);
}
}
class CompanyDemo
{
public static void main(String[] args)
{
Teacher t = new Teacher("毕向东");
try{
t.lecture();
}
catch(StopException e){
System.out.println(e.toString());
System.out.println("change teacher...");//更换老师
}
}
}




7.数据库异常流程

finally的作用:无论是否有异常发生,都要对资源进行释放。资源释放动作就定义在fianlly代码块中

class App{
public void useDB(){
DBtool d = new DBtool();
try{
d.operate();
}
catch(NoValueException e){

}
}
}
class DBtool throws NoValueException{
public void operate(){
//connect DB
try{
//operate DB
throw new SQLException();
}
catch(SQLException e){
//solve SQLException
throw new NoValueException();
//在数据库工具类中解决数据库的异常,但仍需向调用者抛出没有返回值异常,以告诉调用者
}
finally{
//close DB
}
}
}


8.捕获异常处理的几种组合方式:

1.没有资源要释放,仅做异常处理

try{

}

catch(){

}

2.一try多catch,一般对应的是被调用的函数,抛出多个异常情况,分别处理

try{

}

catch(){

}

catch(){

}

注意:如果catch中的异常类存在子父类,父类异常的catch一定要放在子类异常catch的下面,

否则编译失败。

3.不一定要处理异常,但是有资源要释放

try{

}

finally{

}

4.既处理异常,也要释放资源

try{

}

catch{

}

finally{

}

9.覆盖时的异常

子类方法覆盖父类方法只能抛出父类方法的异常或者该异常的子类;

如果父类方法抛出多个异常,子类只能抛出父类异常的子集;

原则:就是子类的异常必须要在父类的异常处理控制中。

注意:只能try,不能throws的情况:

被覆盖的方法没有抛出异常,那么子类在覆盖时,子类方法中发生了异常就只能try无法throws声明。

interface Inter{
void show();
}
class Demo implements Inter{
void show(){
try{
throw new Exception();
}
catch(Exception e){
//若无法解决
throw new RuntimeException();//将编译时异常转化为运行时异常,就无需在函数生声明
}
}
}








                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐