zoukankan      html  css  js  c++  java
  • day08(异常处理,创建异常,finally,throws和throw的区别)

    异常处理,

        异常的产生

              运行时异常:程序运行中产生的异常;RuntimeException类。

              编译时异常:程序在编译时产生的异常;除了RuntimeException类  其他都是编译时产生的异常。

       处理异常两种方式

        抛出异常:把异常抛给调用者区解决   throws 抛出异常

        捕获异常:编程者自己处理try....catch...finally...

    创建异常,

        1.自己创建sun公司的异常类对象

        

    public class ClassException {
    	public static void main(String[] args) {
    		try {//如果出现异常  用来捕获异常
    			checkScore(110);
    		} catch (Exception e) {
    			e.printStackTrace();//输出 异常类型,异常原因,异常位置。  内部具有多线程
    		}
    	}
    	private static void checkScore(int i) throws RuntimeException {
    		if (i<0||i>100) {
    			throw new RuntimeException("不符合要求");//创建运行时异常对象
    		}
    		System.out.println("符合要求");
    	}
    }
    

      

    输出结果 :
    java.lang.RuntimeException: 不符合要求
    	at day08.ClassException.checkScore(ClassException.java:28)
    	at day08.ClassException.main(ClassException.java:21)
    

      

        2.自定异常类继承sun公司的异常类(异常名字可以自定义)

      定义一个自定义类 (MyException)

    public class NotToPrintException  extends Exception{//继承Exception类  调用父类构造输出异常信息
    	public NotToPrintException() {
    		super();
    	}
    	public NotToPrintException(String message) {
    		super(message);
    	}
    }
    

      

    public class ClassException {
    	public static void main(String[] args) {
    		try {
    			checkScore(110);
    		} catch (Exception e) {
    			e.printStackTrace();//输出异常类型,异常原因,异常位置  内部具有多线程
     } }
          //使用自定义异常类来抛出异常 private static void checkScore(int i) throws NotToPrintException {//使用自定义类捕获异常 if (i<0||i>100) { throw new NotToPrintException("不符合要求");//抛出NotToPrintException对象 } System.out.println("符合要求"); } }

      

    day08.NotToPrintException: 不符合要求
    	at day08.ClassException.checkScore(ClassException.java:29)
    	at day08.ClassException.main(ClassException.java:21)
    

      

    finally,

        作用: 做为程序的善后的工作  

        执行顺序:try和catch之后  

      

    public class FinallyTest {
    	public static void main(String[] args) {
    		FileWriter fw=null;
    		try {
    			fw=new FileWriter("a.txt");
    			fw.write("hello");
    			fw.write("world");
    			System.out.println(10/0);//报异常  但是fw还没有关闭   数据没有写进去
    			fw.write("java");
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (ArithmeticException e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				if (fw!=null) {
    					fw.close();//如果中途出现错误  但为了让数据已经写的数据存在文本中  则在finally中进行关闭流操作。
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

      

    a.txt文件中内容


    helloworld

      

                     特殊案例:catch中存在return 和System.exit(0);

      

    public class FinallyTest {
    	public static void main(String[] args) {
    	System.out.println(method1());
    	
    	}
    	private static int method1(){
    		int a=10;
    		try{
    			System.out.println(10/0);
    			a=20;
    		}catch(Exception e){
    			a=30;
    			return a;//在执行finally之前会  return a时 这时a已经被赋值为30  再执行finally中a=40不会影响return a中的a值
          }finally{ a=40; } return a; } }
    输出结果:
    30

    /*
    * return a在程序执行到这一步的时候,这里不是return a而是return 30;这个返回路径就形成了。
    * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
    * 再次回到以前的返回路径,继续走return 30;
    */

        

    public class FinallyTest {
    	public static void main(String[] args) {
    	System.out.println(method1());
    	
    	}
    	private static int method1(){
    		int a=10;
    		try{
    			System.out.println(10/0);
    			a=20;
    		}catch(Exception e){
    			a=30;
    			System.out.println(a);
    			System.exit(0);//程序退出   不会执行finally
             }finally{ System.out.println(1); a=40; } return a; }

      

    throws和throw的区别

      throws:

          位置:方法声明上

          作用:抛出异常  (处理异常的一种方式)

        throw:

          位置:方法中

          作用:创建一个异常对象 (制造异常)

    public class ClassException {
    	public static void main(String[] args) {
    		try {
    			checkScore(110);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		checkScore(10);
    	}
    	private static void checkScore(int i) throws RuntimeException {//抛出异常  抛给调用者进行处理
    		if (i<0||i>100) {
    			throw new RuntimeException("不符合要求");//制造异常
    		}
    		System.out.println("符合要求");
    	}
    }
    

      

         

  • 相关阅读:
    阿里云 Linux Centos7下安装Tomcat8
    Centos7.5中安装JDK1.8环境变量配置
    JAVA中的抽象类和接口
    Struts2框架
    [机器学习]--逻辑回归总结
    matlab-使用技巧
    机器学习-一对多(多分类)代码实现(matlab)
    机器学习-反向传播算法(BP)代码实现(matlab)
    线性回归代码实现(matlab)
    217. Contains Duplicate
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/Try_Catch_Finally.html
Copyright © 2011-2022 走看看