/** * 程序中的异常 * 程序本身有运行异常时,程序是可以编译通过的 */ public void div(){ int sum = 0;
for(int i = 10; i >= -10; i --){ sum = sum + 5/i;//当i=0出现异常 java.lang.ArithmeticException: / by zero 程序终止运行 System.out.println("sum:" + sum + " i:" + i); }
System.out.println("sum:" + sum); }
/** * 程序中有非运行异常抛出的时候,必须要去处理或推卸,否则无法通过编译 */ public void div1(){//throws Exception{
//定义字符输入流 // BufferedReader in = new BufferedReader(new FileReader(new File("StudyException.java")));//未报告的异常 java.io.FileNotFoundException
} /** * 从程序中直接抛出运行异常 */ public void div2(){ throw new RuntimeException("直接抛出运行异常");//此处跟sum = sum + 5/i;一样,都抛出运行异常 // System.out.println("这里执行不了,也无法通过编译");//无法访问的语句 } /////////////////////因为还没有学到相关的非运行异常,所以以下的例子都是用运行异常去模拟非运行异常//////////////////////// /** * 方法对异常的处理(完全抛出)[throws加s表示忽略一类异常] * div3去做一些事情,出现的一切问题都由调用者来承担 */ // public void div3() throws ArithmeticException{//因为抛出了ArithmeticException[运行异常],所以调用不处理可以通过编译 public void div3() throws Exception{//忽略掉所有的非运行异常[调用者必须要进行捕捉或忽略,否则无法通过编译]
//程序中可能出现问题 int sum = 0;
for(int i = 10; i >= -10; i --){ sum = sum + 5/i;//当i=0出现异常 java.lang.ArithmeticException: / by zero 程序终止运行 System.out.println("sum:" + sum + " i:" + i); } }
/** * 方法对异常的处理(完全承担)try catch */ public void div4(){
//程序中可能出现问题 int sum = 0;
try{ for(int i = 10; i >= -10; i --){ sum = sum + 5/i; System.out.println("sum:" + sum + " i:" + i); } }catch(Exception ex){//只要catch就表示捕捉了 System.err.println("都是我的问题,所以我来承担,跟调用者没有关系"); } System.out.println("问题被解决了,这里依然可以正常执行"); System.out.println("sum:" + sum); } /** * 方法对异常的处理(完全承担)[缩小处理范围] try catch */ public void div5(){
//程序中可能出现问题 int sum = 0;
for(int i = 10; i >= -10; i --){//每个数值去try一次 try{ sum = sum + 5/i; System.out.println("sum:" + sum + " i:" + i); }catch(Exception ex){//只要catch就表示捕捉了 System.err.println("都是我的问题,所以我来承担,跟调用者没有关系"); } }
int sum = 0; for(int i = 10; i >= -10; i --){ sum = sum + 5/i; System.out.println("sum:" + sum + " i:" + i); } }catch(ArithmeticException ae){ System.err.println("数学运算错了"); }catch(ArrayIndexOutOfBoundsException aiobe){ System.err.println("索引超界了"); } System.out.println("问题被解决了,这里可以继续运行了"); }
/** * 异常的部分承担 */ public void div7() throws Exception{//因为从方法中抛出了非运行异常,所以这里必须要声明抛出[忽略] try{ int[] intArray = new int[10]; intArray[10] = 100;//java.lang.ArrayIndexOutOfBoundsException: 10 System.out.println("上边出问题了,这里不会被执行了");
int sum = 0; for(int i = 10; i >= -10; i --){ sum = sum + 5/i; System.out.println("sum:" + sum + " i:" + i); } }catch(ArithmeticException ae){//这个异常是我的问题 System.err.println("数学运算错了"); }catch(ArrayIndexOutOfBoundsException aiobe){//这个异常跟我无关,所以我要抛出去 System.err.println("索引超界了"); //throw aiobe;//类型是ArrayIndexOutOfBoundsException[运行异常]//方法体上不需要声明抛出(忽略异常 throws) throw new Exception("这个问题和我无关,我推卸掉", aiobe); } System.out.println("一旦throw[问题抛出],这里就不会运行了,如果异常都被处理了,这里会被运行"); }
/** * 用超类来捕捉异常 */ public void div8(){ try{ int[] intArray = new int[10]; intArray[10] = 100;
int sum = 0; for(int i = 10; i >= -10; i --){ sum = sum + 5/i; System.out.println("sum:" + sum + " i:" + i); } }catch(ArithmeticException ae){//已捕捉到异常 java.lang.ArithmeticException System.err.println("数学运算错了"); }catch(Exception ex){//超类异常必须放在后边,如果放在前边拦截了子异常,则无法通过编译 System.err.println("这里捕捉到没有考虑到的因素"); } }
/** * finally程序中必须要处理的事情,通常用来关闭资源 */ public void div9() throws Exception{ try{ int[] intArray = new int[10]; intArray[10] = 100;
int sum = 0; for(int i = 10; i >= -10; i --){ sum = sum + 5/i; System.out.println("sum:" + sum + " i:" + i); } /** int[] intArray = new int[10]; intArray[10] = 100; */ }catch(ArithmeticException ae){ System.err.println("数学运算错了"); }catch(Exception ex){ System.err.println("索引超界了"); //抛出异常 throw new Exception("索引超界了", ex);//有return效果跟throw一样 //这里无法被访问 }finally{ System.out.println("无论如何都会执行这里的代码"); } System.out.println("如果程序中问题被解决了,这里会被执行"); }
/** * 获得和传递异常的信息 */ public void div10() throws Exception{ try{ int sum = 0; for(int i = 10; i >= -10; i --){ sum = sum + 5/i; System.out.println("sum:" + sum + " i:" + i); }
int[] intArray = new int[10]; intArray[10] = 100; }catch(Exception ex){ //获得异常的信息 String message = ex.getMessage(); System.err.println("Message:" + message); System.out.println("---------------------"); //获得异常栈 ex.printStackTrace(); System.out.println("---------------------"); //抛出异常 //throw ex;//将异常传递到外层 //throw new Exception(ex); //throw new Exception("出问题了!");//转化为新的异常对象抛出,原来的异常信息不被外层知道 throw new Exception("出问题了!", ex); } }
/** * 异常对返回值的影响 * 1、有问题直接抛出异常 */ public int div11() throws Exception{
int sum = 0; try{ for(int i = 10; i >= -10; i --){ sum = sum + 5/i; } return sum;//正常返回结果值 }catch(Exception ex){ //有问题的时候抛出问题 System.err.println("出问题了"); throw ex; // return sum;//这里的代码永远不会被访问 } // return sum;//正常返回结果值 } /** * 异常对返回值的影响 * 2、有问题转化为自定义对象返回 */ public ResultInfo div12() throws Exception{
int sum = 0; try{ for(int i = 10; i >= -10; i --){ sum = sum + 5/i; } return new ResultInfo(ResultInfo.RESULT_SUCCESS, "成功了"); }catch(Exception ex){ return new ResultInfo(ResultInfo.RESULT_FAILURE, "失败了"); } }
/** * try catch多层嵌套 */ public void div13(){
int sum = 0; try{ try{ for(int i = 10; i >= -10; i --){ sum = sum + 5/i; } }catch(ArrayIndexOutOfBoundsException ex){ System.err.println("索引超界了"); } }catch(Exception ex){ System.err.println("其他问题"); } } /** * 方法的多层嵌套(div14和div15) */ public void div14(){ int sum = 0; try{ for(int i = 10; i >= -10; i --){ sum = sum + 5/i; } }catch(ArrayIndexOutOfBoundsException ex){ System.err.println("索引超界了.."); //throw ***Exception; } } public void div15(){ try{ this.div14(); }catch(Exception ex){ System.err.println("其他问题.."); //throw ***Exception; } }
/** * 使用自定义异常 */ public void div16(int min, int max) throws ParameterValidException, DataParseException{
if(min <= 0 && max >= 0){ //参数非法 System.err.println("参数非法"); throw new ParameterValidException("参数非法"); }
int sum = 0; try{ for(int i = min; i <= max; i ++){ sum = sum + 5/i; }
int[] myArray = new int[max];//max>=11 myArray[10] = 20; }catch(Exception ex){ //数据错误了 System.err.println("数据错误了"); throw new DataParseException("数据错误了", ex); } } /** * 使用自定义异常场景 */ public void div17(int min, int max) throws DataParseException{ //后来发现方法里有很多异常不属于我自己的 //转化自定义异常DataParseException }
我写个句子你去运行下!
String s="hello";
int n=Integer.parseInt(s);
放到main方法里面运行,出错了吧!
然后你加个try-catch试下
try{
String s="hello";
int n=Integer.parseInt(s);
}catch(Exception e){
System.out.println("逮到错误了,所以我就执行了!");
}
相信你能理解的吧!
public static Date getDate(String source, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(source);//这里可能碰到很多问题,比如输入空字符串,这时候就不能正常转换就会抛出一个异常
} catch (ParseException e) {
return null;
}
}
但是有些时候的捕捉是对于不小心的错误进行捕捉的,就好像我上面的s,现在我是通过手动输入的,正常情况下我输入的都是数字,但是有一天我不小心输入了个字母,这个时候就出错,但为了能让我知道他的错误情况,所以就需要加个try-catch了!
/**
* 程序中的异常
* 程序本身有运行异常时,程序是可以编译通过的
*/
public void div(){
int sum = 0;
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;//当i=0出现异常 java.lang.ArithmeticException: / by zero 程序终止运行
System.out.println("sum:" + sum + " i:" + i);
}
System.out.println("sum:" + sum);
}
/**
* 程序中有非运行异常抛出的时候,必须要去处理或推卸,否则无法通过编译
*/
public void div1(){//throws Exception{
//定义字符输入流
// BufferedReader in = new BufferedReader(new FileReader(new File("StudyException.java")));//未报告的异常 java.io.FileNotFoundException
} /**
* 从程序中直接抛出运行异常
*/
public void div2(){
throw new RuntimeException("直接抛出运行异常");//此处跟sum = sum + 5/i;一样,都抛出运行异常
// System.out.println("这里执行不了,也无法通过编译");//无法访问的语句
} /////////////////////因为还没有学到相关的非运行异常,所以以下的例子都是用运行异常去模拟非运行异常//////////////////////// /**
* 方法对异常的处理(完全抛出)[throws加s表示忽略一类异常]
* div3去做一些事情,出现的一切问题都由调用者来承担
*/
// public void div3() throws ArithmeticException{//因为抛出了ArithmeticException[运行异常],所以调用不处理可以通过编译
public void div3() throws Exception{//忽略掉所有的非运行异常[调用者必须要进行捕捉或忽略,否则无法通过编译]
//程序中可能出现问题
int sum = 0;
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;//当i=0出现异常 java.lang.ArithmeticException: / by zero 程序终止运行
System.out.println("sum:" + sum + " i:" + i);
}
}
/**
* 方法对异常的处理(完全承担)try catch
*/
public void div4(){
//程序中可能出现问题
int sum = 0;
try{
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
System.out.println("sum:" + sum + " i:" + i);
}
}catch(Exception ex){//只要catch就表示捕捉了
System.err.println("都是我的问题,所以我来承担,跟调用者没有关系");
}
System.out.println("问题被解决了,这里依然可以正常执行");
System.out.println("sum:" + sum);
} /**
* 方法对异常的处理(完全承担)[缩小处理范围] try catch
*/
public void div5(){
//程序中可能出现问题
int sum = 0;
for(int i = 10; i >= -10; i --){//每个数值去try一次
try{
sum = sum + 5/i;
System.out.println("sum:" + sum + " i:" + i);
}catch(Exception ex){//只要catch就表示捕捉了
System.err.println("都是我的问题,所以我来承担,跟调用者没有关系");
}
}
System.out.println("问题被解决了,这里依然可以正常执行");
System.out.println("sum:" + sum);
}
/**
* 多种异常的抛出
*/
public void div6(){
try{
int[] intArray = new int[10];
intArray[10] = 100;//java.lang.ArrayIndexOutOfBoundsException: 10
System.out.println("上边出问题了,这里不会被执行了");
int sum = 0;
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
System.out.println("sum:" + sum + " i:" + i);
}
}catch(ArithmeticException ae){
System.err.println("数学运算错了");
}catch(ArrayIndexOutOfBoundsException aiobe){
System.err.println("索引超界了");
}
System.out.println("问题被解决了,这里可以继续运行了");
}
/**
* 异常的部分承担
*/
public void div7() throws Exception{//因为从方法中抛出了非运行异常,所以这里必须要声明抛出[忽略] try{
int[] intArray = new int[10];
intArray[10] = 100;//java.lang.ArrayIndexOutOfBoundsException: 10
System.out.println("上边出问题了,这里不会被执行了");
int sum = 0;
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
System.out.println("sum:" + sum + " i:" + i);
}
}catch(ArithmeticException ae){//这个异常是我的问题
System.err.println("数学运算错了");
}catch(ArrayIndexOutOfBoundsException aiobe){//这个异常跟我无关,所以我要抛出去
System.err.println("索引超界了");
//throw aiobe;//类型是ArrayIndexOutOfBoundsException[运行异常]//方法体上不需要声明抛出(忽略异常 throws)
throw new Exception("这个问题和我无关,我推卸掉", aiobe);
}
System.out.println("一旦throw[问题抛出],这里就不会运行了,如果异常都被处理了,这里会被运行");
}
/**
* 用超类来捕捉异常
*/
public void div8(){ try{
int[] intArray = new int[10];
intArray[10] = 100;
int sum = 0;
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
System.out.println("sum:" + sum + " i:" + i);
}
}catch(ArithmeticException ae){//已捕捉到异常 java.lang.ArithmeticException
System.err.println("数学运算错了");
}catch(Exception ex){//超类异常必须放在后边,如果放在前边拦截了子异常,则无法通过编译
System.err.println("这里捕捉到没有考虑到的因素");
}
}
/**
* finally程序中必须要处理的事情,通常用来关闭资源
*/
public void div9() throws Exception{
try{ int[] intArray = new int[10];
intArray[10] = 100;
int sum = 0;
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
System.out.println("sum:" + sum + " i:" + i);
}
/**
int[] intArray = new int[10];
intArray[10] = 100;
*/
}catch(ArithmeticException ae){
System.err.println("数学运算错了");
}catch(Exception ex){
System.err.println("索引超界了");
//抛出异常
throw new Exception("索引超界了", ex);//有return效果跟throw一样
//这里无法被访问
}finally{
System.out.println("无论如何都会执行这里的代码");
}
System.out.println("如果程序中问题被解决了,这里会被执行");
}
/**
* 在实际开发当中,运行异常是我们应该避免的,避免了所以就不需要用catch去捕捉
* 而对于非运行异常,是我们必须要用catch处理的,要么声明抛出
*/
/**
* 获得和传递异常的信息
*/
public void div10() throws Exception{ try{
int sum = 0;
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
System.out.println("sum:" + sum + " i:" + i);
}
int[] intArray = new int[10];
intArray[10] = 100; }catch(Exception ex){
//获得异常的信息
String message = ex.getMessage();
System.err.println("Message:" + message);
System.out.println("---------------------");
//获得异常栈
ex.printStackTrace();
System.out.println("---------------------");
//抛出异常
//throw ex;//将异常传递到外层
//throw new Exception(ex);
//throw new Exception("出问题了!");//转化为新的异常对象抛出,原来的异常信息不被外层知道
throw new Exception("出问题了!", ex);
}
}
/**
* 异常对返回值的影响
* 1、有问题直接抛出异常
*/
public int div11() throws Exception{
int sum = 0;
try{
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
} return sum;//正常返回结果值
}catch(Exception ex){
//有问题的时候抛出问题
System.err.println("出问题了");
throw ex;
// return sum;//这里的代码永远不会被访问
}
// return sum;//正常返回结果值
}
/**
* 异常对返回值的影响
* 2、有问题转化为自定义对象返回
*/
public ResultInfo div12() throws Exception{
int sum = 0;
try{
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
} return new ResultInfo(ResultInfo.RESULT_SUCCESS, "成功了");
}catch(Exception ex){
return new ResultInfo(ResultInfo.RESULT_FAILURE, "失败了");
}
}
/**
* try catch多层嵌套
*/
public void div13(){
int sum = 0;
try{
try{
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
}
}catch(ArrayIndexOutOfBoundsException ex){
System.err.println("索引超界了");
}
}catch(Exception ex){
System.err.println("其他问题");
}
} /**
* 方法的多层嵌套(div14和div15)
*/
public void div14(){
int sum = 0;
try{
for(int i = 10; i >= -10; i --){
sum = sum + 5/i;
}
}catch(ArrayIndexOutOfBoundsException ex){
System.err.println("索引超界了..");
//throw ***Exception;
}
}
public void div15(){
try{
this.div14();
}catch(Exception ex){
System.err.println("其他问题..");
//throw ***Exception;
}
}
/**
* 使用自定义异常
*/
public void div16(int min, int max) throws ParameterValidException, DataParseException{
if(min <= 0 && max >= 0){
//参数非法
System.err.println("参数非法");
throw new ParameterValidException("参数非法");
}
int sum = 0;
try{
for(int i = min; i <= max; i ++){
sum = sum + 5/i;
}
int[] myArray = new int[max];//max>=11
myArray[10] = 20;
}catch(Exception ex){
//数据错误了
System.err.println("数据错误了");
throw new DataParseException("数据错误了", ex);
}
}
/**
* 使用自定义异常场景
*/
public void div17(int min, int max) throws DataParseException{
//后来发现方法里有很多异常不属于我自己的
//转化自定义异常DataParseException
}