1:异常的捕捉
try和catch
出现try就必须出现catch不然就报错
既可以用该异常类(FileNotFoundException)捕获异常
也可以用改类的父类捕获异常(Exception)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class zhulei {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f = new File("d:/LOL.exe");
try
{
System.out.println("尝试打开lol.exe文件");
new FileInputStream(f);
System.out.println("打开成功");
}
// catch(FileNotFoundException e)
catch(Exception e)
{
System.out.println("lol.exe文件不存在");
e.printStackTrace();//打印出方法调用的痕迹
}
}
}
2:多异常的捕获
一个try匹配多个catch
finally最后肯定会执行的代码
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class zhulei {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f = new File("d:/LOL.exe");
try {
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2016/06-03");
System.out.println(d.toString());
}
catch(FileNotFoundException e)
{
System.out.println("文件打开失败");
e.printStackTrace();
}
catch(ParseException e)
{
System.out.println("日期格式解析错误");
e.printStackTrace();
}
finally
{
System.out.println("最后肯定会执行的代码");
}
}
}
3:throws关键字的使用
throws关键字使产生异常的方法将异常从该方法中抛出,抛到调用它的方法中,由接受到异常的方法来进行调用。
throws与throw这两个关键字接近,不过意义不一样,有如下区别:
- throws 出现在方法声明上,而throw通常都出现在方法体内。
- throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某个异常对象。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class zhulei {
public static void main(String[] args) {
method1();
}
private static void method1()
{
try {
method2();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
private static void method2() throws FileNotFoundException
{
File f = new File("d:/LOL.exe");
System.out.println("试图打开lol.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
}
4:练习
最终还是执行finally的语句
如果不知到异常的具体种类,就用Exception代替
public class zhulei {
public static void main(String[] args) {
zhulei a = new zhulei();
System.out.println(a.method());//3
}
public int method()
{
try
{
return 1;
}
catch(Exception e)//如果不知到异常的具体种类,就用Exception代替
{
return 2 ;
}
finally
{
return 3;
}
}
}
5:异常的分类
异常指的是在程序执行过程中,出现非正常的情况,导致jvm非正常停止。
在java中异常是一个类,产生异常就是创建异常对象并抛出一个异常对象。
a.可查异常
即必须进行处理的异常,要么try catch住,要么往外抛,谁调用,谁处理,比如 FileNotFoundException
不处理编译器就不让你通过
b.运行时异常
RuntimeException指: 不是必须进行try catch的异常
常见运行时异常:
除数不能为0异常:ArithmeticException
下标越界异常:ArrayIndexOutOfBoundsException
空指针异常:NullPointerException
在编写代码的时候,依然可以使用try catch throws进行处理,与可查异常不同之处在于,即便不进行try catch,也不会有编译错误
c.错误
错误Error,指的是系统级别的异常,通常是内存用光了
在默认设置下,一般java程序启动的时候,最大可以使用16m的内存
如例不停的给StringBuffer追加字符,很快就把内存使用光了。抛出OutOfMemoryError
与运行时异常一样,错误也是不要求强制捕捉的
public class zhulei {
public static void main(String[] args) {
StringBuffer a =new StringBuffer();
for(int i =0;i<Integer.MAX_VALUE;i++)
{
a.append('a');
}
}
}
总结
错误和运行时异常均可被捕捉
Stackoverflow堆栈溢出
ArithmeticException算数运算异常
eoFE读取异常
运行时异常与非运行时异常的区别
运行时异常是不可查异常,不用显示捕捉。
非运行时异常是可查异常,需要对其进行显示的捕捉。不然编译器不让通过。
6:Throwable类
异常的根类是java.lang.Throwable类。
Error和Exception类继承了Throwable类。
我们平时说的异常指的是Exception。
5:异常的分类
异常指的是在程序执行过程中,出现非正常的情况,导致jvm非正常停止。
在java中异常是一个类,产生异常就是创建异常对象并抛出一个异常对象。
Throwable分为Error和Exception。
Exception分为编译期异常和运行时异常。
a.编译期异常(可查异常)
即必须进行处理的异常,要么try catch住,要么往外抛,谁调用,谁处理,比如 FileNotFoundException
不处理编译器就不让你通过
b.运行期异常
RuntimeException指: 不是必须进行try catch的异常
常见运行时异常:
除数不能为0异常:ArithmeticException
下标越界异常:ArrayIndexOutOfBoundsException
空指针异常:NullPointerException
在编写代码的时候,依然可以使用try catch throws进行处理,与可查异常不同之处在于,即便不进行try catch,也不会有编译错误
c.错误
错误Error,指的是系统级别的异常,通常是内存用光了
在默认设置下,一般java程序启动的时候,最大可以使用16m的内存
如例不停的给StringBuffer追加字符,很快就把内存使用光了。抛出OutOfMemoryError
与运行时异常一样,错误也是不要求强制捕捉的
所以不知道具体异常种类时,不仅可以用Exception也可以用Throwable。
7:throw关键字的使用****且自定义异常
自定义异常则只有该对象是Exception或者其子类之一的实例时,才能被抛出。
throw关键字在方法体上实例化并抛出一个异常。
public class EnemyHeroIsDeadException extends Exception{
public EnemyHeroIsDeadException(String str)
{
super(str);
}
}
public class Hero {
public String name; //姓名
float hp;
public Hero(String name)
{
this.name = name;
}
public void heroAttack(Hero h) throws EnemyHeroIsDeadException
//throws的作用是将该方法有可能产生的异常抛出至调用该方法的方法中去
{
if(h.hp==0)
throw new EnemyHeroIsDeadException(h.name+"已经挂了,不需要再释放技能");
}
}
public class zhulei {
public static void main(String[] args) {
Hero a = new Hero("盖伦");
Hero b = new Hero("被打的");
b.hp=0;
try
{
a.heroAttack(b);
}
catch(EnemyHeroIsDeadException e)
{
System.out.println("异常的具体原因"+e.getMessage());
//getMessage()方法获得出错的具体原因
e.printStackTrace();
}
}
}
8:异常综合练习
#:protected
+:public
-:private
下划线:构造方法
public class OverDrafException extends Exception{
private double deficit;//透支额度
public OverDrafException(String message,double deficit)
{
super(message);//自定义异常一般都有这一步
this.deficit = deficit;
}
public double getDeficit()
{
return deficit;
}
}
public class Account {
protected double balance;
public Account(double init)
{
this.balance = init;
System.out.println("账户创建成功"+"余额为"+this.balance);
}
public double getBalance()
{
return balance;
}
public void deposit(double amt)
{
this.balance = this.balance + amt;
}
public void withdraw(double amt)throws OverDrafException
{
if(amt>this.balance)
{
throw new OverDrafException("余额不足",this.balance-amt);
}
else
{
this.balance = this.balance - amt;
System.out.println("取款成功,账户余额为"+this.balance+"元");
}
}
}
```java
public class CheakingAccount extends Account {
private double overdrafProtection;//透支额度
public CheakingAccount(double init) {
super(init);
// TODO Auto-generated constructor stub
}
public CheakingAccount(double init,double protect)
{
super(init);
this.overdrafProtection = protect;
}
public void withDraw(double amt) throws OverDrafException//将new OverDrafException异常抛到调用它的地方
//也就是zhulei中的try catch语句
{
if(amt>this.balance+overdrafProtection)
{
throw new OverDrafException("超过可透支余额",amt-this.balance-overdrafProtection);
}
else
{
this.balance = this.balance -amt;
System.out.println("取款成功,剩余金额为"+this.balance);
}
}
}
public class zhulei {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account a = new Account(100);
try
{
a.withdraw(10);
a.withdraw(200);
}
catch(OverDrafException e)
{
System.out.println(e.getDeficit());
e.printStackTrace();
}
CheakingAccount b = new CheakingAccount(100,100);
try
{
b.withDraw(120);
b.withDraw(80);
b.withDraw(80);
}
catch(OverDrafException e)
{
e.printStackTrace();
}
}
}