Java知识(异常)

来源:互联网 发布:cctv网络电视播放器 编辑:程序博客网 时间:2024/06/09 14:08
1.异常处理
异常就是在程序的运行过程中所发生的不正常事件,它会中断正在运行的程序。
异常处理:java编程语言使用异常处理机制为程序提供了错误处理的功能。
五个关键字:
1.try:包裹可能出现bug(异常)的代码
2.catch:发生异常时,捕获异常
3.finally:不管程序有没有异常,最终都要执行finally代码,finally是最终执行的代码块
public class Ch01 {
public static void main(String[] args) {
//包裹可能出现bug(异常)的代码
try{
Scanner scanner=new Scanner(System.in);
System.out.println("请输入被除数");
int a=scanner.nextInt();
System.out.println("请输入除数");
int b=scanner.nextInt();
System.out.println("相除结果是"+a/b);
}catch(InputMismatchException e){
//输入内容不匹配的情况,就会进入这个异常处理
System.out.println("输入的是非整数,谢谢使用");
}catch (ArithmeticException e1) {
// 算术出现错误异常时,会进入这个异常处理
System.out.println("除数不能为0,谢谢使用");
}catch (Exception e2) {
// 除了上边出现的错误以外,其他任何异常发生就会进入这个 异常处理
}finally {
//不管程序有没有异常,最终都要执行finally代码
//finally是最终执行的代码块
}
}
}
4.throws:在声明方法中抛出异常,调用者必须解决这些异常(或者再次向上抛出)
public class Ch03 {
/**
* 声明方法可能会出现异常
* 调用者必须解决这些异常(或者再次向上抛出)
* @throws InputMismatchException
* @throws ArithmeticException
* @throws Exception
*/
public static void test() throws InputMismatchException,ArithmeticException,Exception{
Scanner scanner=new Scanner(System.in);
System.out.println("请输入被除数");
int a=scanner.nextInt();
System.out.println("请输入除数");
int b=scanner.nextInt();
System.out.println("相除结果是"+a/b);
}
public static void main(String[] args) {
try {
test();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

5.throw:手动抛出异常;程序本身可能没有异常,但是不符合业务逻辑时,可以手动抛出异常
public class Ch04 {
/**
* throw作用:手动抛出异常
* 程序本身可能没有异常,但是不符合业务逻辑时
* 可以手动抛出异常
*/
public static void test(){
Scanner in=new Scanner(System.in);
System.out.println("请输入被除数");
int a=in.nextInt();
System.out.println("请输入除数");
int b=in.nextInt();
if(b==1) {
throw new CanNotBeOneException();
}
if(b==0) {
throw new ArithmeticException("除数不能为0");
}
System.out.println("相除结果是"+a/b);
}
public static void main(String[] args) {
test();
}
}
/**
* 自定义异常
* @author Administrator
*
*/
class CanNotBeOneException extends RuntimeException{

public CanNotBeOneException() {
super("除数不能为1");
// TODO Auto-generated constructor stub
}
}

拓展:return和finally问题
try{}里有一个return语句,那么紧跟在这个try后的finally{}里的代码会执行,而且是在return之后执行。
public class Ch02 {
public static int test(){
int a;
try{
a=10;
return a;
}finally{
a=20;
}
}
public static void main(String[] args) {
System.out.println(test());
}
}
输出的结果是10
2.多重catch块
一段代码可能会发生多种类型的异常,这时需要多重catch来捕获异常。当异常方生时,会按顺序来查看每个catch语句,并执行第一个与异常类型匹配的catch语句,执行其中一条catch语句后,其后catch语句会被忽略
try {
test();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


3.自定义异常
利用throw、throws来定义异常
常见的异常类型:
 方法名说明Exception异常层次结构的根类ArithmeticException算术错误情形,如以0作为除数ArrayIndexOutBoundsException数组下标越界BullPointerException尝试访问null对象成员ClassNotFoundException不能加载所需的类InputMismatchException欲得到数据类型与实际输入类型不匹配IllegalAccessException方法接收到非法参数ClassCastException对象强制类型转换出错NumberFormatException数字格式转换异常,如吧“abc”转换成数字
通过继承的不同,可以分为check异常和运行时异常
check异常:继承Exception,程序必须要解决这个问题
运行时异常:继承RuntimeException,可以不解决
public class Ch05 {
public void test(int a, int b) {
//计算两数之商
if(a==0) {
//runtime异常,可以不解决
throw new BException();
}
if(b==0) {
//check异常必须解决
// throw new AException();
}
System.out.println(a/b);
}

}
/**
* check异常,程序必须要解决这个问题
* @author Administrator
*
*/
class AException extends Exception{

public AException() {
super();
// TODO Auto-generated constructor stub
}

public AException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}

/**
* 运行时异常,可以不解决
* @author Administrator
*
*/
class BException extends RuntimeException{
public BException() {
super();
// TODO Auto-generated constructor stub
}

public BException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
4.异常处理-注意事项
1.既然捕获了异常,就要对它进行适当的处理。不要捕获异常之后又把它丢弃,不予理睬
2.在catch语句中尽可能指定具体的异常类型,必要时使用多个catch。不要试图处理所有可能出现的异常
3.包证所有资源都被正确释放。充分运用finally关键词
4.在异常处理模块中提供适量的错误原因信息,组织错误信息使其易于理解和阅读
5.尽量减小try块的体积





原创粉丝点击