package j2se; import java.util.Scanner; public class DM002 { public static void main(String[] args) { DT d=new DT(); try{//这里去处理,这里不想处理,就在主函数上边第5行再往上抛,抛给jre d.qq(); } catch(Exception e){ System.out.println("输入有误"); } finally{ System.out.println("程序继续运行中"); } } } class DT{ Scanner in=new Scanner(System.in); public void qq() throws Exception{//抛给调用这个方法的地方去处理,向上抛 int a=in.nextInt(); int b=in.nextInt(); int c=a%b; System.out.println("余数为"+c); } }
======
package j2se; import java.util.*; class DM{ Scanner in=new Scanner(System.in); public void qy(){ try{ int a=in.nextInt(); int b=in.nextInt(); int c=a%b; System.out.println("余数为"+c); } catch(Exception e){ System.out.println("输入有误"); } finally{//finally最后一定会执行 System.out.println("程序运行中"); } } } public class DM001 { public static void main(String[] args) { DM d=new DM(); d.qy(); } }
=====手动一个新的异常
package j2se; public class MyException extends Exception { public MyException(){ } public MyException(String message){ super(message); } }
==============================
package j2se; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TestReadFile { public static void main(String[] args) { String str=new TestReadFile().openFile(); System.out.println(str); } String openFile(){ try{ System.out.println("aaa"); FileInputStream fis=new FileInputStream("d:/a344.txt"); int a=fis.read(); System.out.println("bbb"); return "step1";//先确定返回的值,并不会直接结束运行 }catch(FileNotFoundException e){ System.out.println("catching!!!!!!!"); return "step2"; }catch(IOException e){ e.printStackTrace(); return "step3"; }finally{ System.out.println("finally"); //return "fff";//不要在finally是使用return } } }
try catch finally return 执行顺序
1.先执行try catch ,给返回值赋值
2.执行finally
3.return