异常处理格式
在Java 中使用 try....catch 进行异常的处理
try{
可能出现异常的语句
}catch(异常类 异常对象){
异常处理
}catch(异常类 异常对象){
异常处理
}catch(异常类 异常对象){
异常处理
}。。。
finally{
异常的出口;
}
package org.exceptiondemo;
public class ExceptionDemo02 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
int j = 0;
System.out.println("******* 计算开始 *******");
try{
System.out.println("计算结果:"+i/j);
}catch(ArithmeticException e){
System.out.println("出现了数学异常:"+e);
}
System.out.println("******* 计算结束 *******");
}
}
package org.exceptiondemo;
public class ExceptionDemo01{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
int j = 0;
System.out.println("******* 计算开始 *******");
try{
System.out.println("计算结果:"+i/j);
}catch(ArithmeticException e){
System.out.println("异常原因:"+e);
}finally{
System.out.println("不管是否有异常,我都执行");
}
System.out.println("******* 计算结束 *******");
}
}
package org.exceptiondemo;
public class ExceptionDemo02{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0 ;
int j = 0;
System.out.println("******* 计算开始 *******");
try{
i = Integer.parseInt(args[0]);
j = Integer.parseInt(args[1]);
System.out.println("计算结果:"+i/j);
}catch(ArithmeticException e){
System.out.println("出现了数据学异常:"+e);
}catch(NumberFormatException e){
System.out.println("输入的不是数字:"+e);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("输入的参数个数不对:"+e);
}finally{
System.out.println("不管是否有异常,我都执行");
}
System.out.println("******* 计算结束 *******");
}
}
那么寻于本程序来讲,实际上还是有可能出现其他异常的。
异常处理流程
1. 对于java 来讲,每当程序中出现了异常,实际上都是产生了一个异常类的实例化对象
这种处理格式实际上非常类似于方法传参,只要参数类型匹配了,则就可以使用此catch进行处理
实际上异常处理的最大父类:Throwable,但是一般开发中不会使用此种方式处理,因为其下有两个子类
Error: Error 一般表示JVM错误,与程序无关
Exception:一般指的是程序中的错误,所以一般开发中如果要想进行程序的处理,其本上是使用此类表示.
一般来讲在程序捕获的时候不要出现Throwable,因为表示的范围太大了
package org.exceptiondemo;
public class ExceptionDemo02{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0 ;
int j = 0;
System.out.println("******* 计算开始 *******");
try{
i = Integer.parseInt(args[0]);
j = Integer.parseInt(args[1]);
System.out.println("计算结果:"+i/j);
}catch(ArithmeticException e){
System.out.println("出现了数据学异常:"+e);
}catch(NumberFormatException e){
System.out.println("输入的不是数字:"+e);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("输入的参数个数不对:"+e);
}catch(Exception e){
System.out.println("其它异常:"+e);
}finally{
System.out.println("不管是否有异常,我都执行");
}
System.out.println("******* 计算结束 *******");
}
}
但是,在异常捕获的时候,有一个注意:捕获更细的异常需要放在捕获更粗的异常之前
package org.exceptiondemo;
public class ExceptionDemo02{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0 ;
int j = 0;
System.out.println("******* 计算开始 *******");
try{
i = Integer.parseInt(args[0]);
j = Integer.parseInt(args[1]);
System.out.println("计算结果:"+i/j);
}catch(Exception e){
System.out.println("出现了数据学异常:"+e);
}finally{
System.out.println("不管是否有异常,我都执行");
}
System.out.println("******* 计算结束 *******");
}
}
throws 关键字
在程序的方法声明处可以使用throws 关键字,使用此关键字的最大好处:在方法中不处理任何的异常,而交给被调用处处理。
package org.exceptiondemo;
class Math{
public int div(int i,int j)throws Exception{
int temp = i/j;
return temp;
}
}
public class ExceptionDemo03 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Math mt = new Math();
try{
mt.div(10, 0);
}catch(Exception e){
System.out.println("异常:"+e);
//e.printStackTrace(); 异常明细
}
}
}
package org.exceptiondemo;
class Math{
public int div(int i,int j)throws Exception{
int temp = i/j;
return temp;
}
}
public class ExceptionDemo03 {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Math mt = new Math();
mt.div(10, 0);
}
}
如果在主方法处使用了throws 关键字的话,则所有的异常交给最大的头:JVM进行处理
throw 关键字
在程序中可以使用throw 关键字人为抛出一个异常
在异常的处理中,实际上每次产生异常的时候都是产生了一个异常类的实例化对象。那么此时,也可以通过抛出异常对象的方式完成。
package org.exceptiondemo;
public class ExceptionDemo04 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
throw new Exception("抛着玩"); //人为抛出
}catch(Exception e){
e.printStackTrace();
}
}
}
异常的标准格式
进入方法执行计算前要有输出
此方法执行完之后也要有输出
如果有异常,则交给被调用处处理
package org.exceptiondemo;
class Math1{
public int div(int i,int j)throws Exception{
int temp = 0;
System.out.println("计算前显示-------------");
try{
temp = i/j;
}catch(Exception e){
throw e;
}finally{
System.out.println("计算后显示-------------");
}
return temp;
}
}
public class ExceptionDemo05 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Math1 mt = new Math1();
try{
System.out.println(mt.div(10,0));
}catch(Exception e){
e.printStackTrace();
}
}
}
自定义异常
一个类只要继承了Exception则就表示一个自定义异常,当发现系统中提供的异常不够的时候就可以这样做
package org.exceptiondemo;
class MyException extends Exception{
public MyException (String msg){
super(msg);
}
}
public class MyExceptionDemo01 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
throw new MyException("自定义异常");
}catch(Exception e){
e.printStackTrace();
}
}
}