zoukankan      html  css  js  c++  java
  • 学习java第15天

    1.异常

    又称为例外,差错,违例,对应java运行错误处理机制

    *基本写法

    try{

        语句组

    }catch(Exception ex){

        异常处理语句组;

    }

    import java.io.*;
    public class ExceptionForNum
    {
     public static void main(String[] args)
     {
      try{
       BufferedReader in = new BufferedReader(
        new InputStreamReader( System.in ) );
       System.out.print("Please input a number: ");
       String s = in.readLine();
       int n = Integer.parseInt( s );
      }catch(IOException ex){
       ex.printStackTrace();
      }catch(NumberFormatException ex){
       ex.printStackTrace();
      }
     }
    }

    2.java中的异常处理

    抛出异常   运行时系统在调用栈中查找   获取异常的代码

    *抛出异常

    throw 异常对象;

    *捕获异常

    try{

        语句组

    }catch(异常类名 异常形式参数名){

        异常处理语句组;

    }

    catch(异常类名 异常形式参数名){

        异常处理语句组;

    }finally{

        异常处理语句组;

    }        //catch语句可0至多个,可以没有finally语句

    3.异常的分类

    Throwable 

    *Error:JVM的错误    Exception:异常           //一般所说的异常,是指Exception及其子类

    4.Exception类

    *构造方法

    public Exception();

    public Exception(String message);

    Exception(String message,Throwable cause);

    *方法

    getMessage()       getCause()        printStackTrace()

    5.多异常处理

    *子类异常要排在父类异常前面

    *finally语句

    无论是否异常都要执行,即使有break,return等语句,在编译时,finally部分代码生成了多遍

    public class TestTryFinally {
        public static String output = "";
        public static void foo(int i) {
            try {
                if (i == 1) {
                    throw new Exception();
                }
                output += "1";
            } catch(Exception e) {
                output += "2";
                return;
            } finally {
                output += "3";
            }
            output += "4";
        }
        public static void main(String args[]) {
            //foo(0);
            //System.out.print(output + " ");
            foo(1);
            System.out.println(output);
        }
    }
     
     
     
    6受检的异常
    *Exception分两种,RuntimeException及其子类,可不明确处理,否则,称为异常的处理
    *受检的异常,要求明确进行语法处理,要么抛,要么捕
    import java.io.*;
    public class ExceptionTrowsToOther{
     public static void main(String[] args){
     
      try{
       System.out.println("====Before====");
        readFile();
       System.out.println("====After====");
       }catch(IOException e){ System.out.println(e); }
     }
     public static void readFile()throws IOException {
      FileInputStream in=new FileInputStream("myfile.txt");
      int b; 
      b = in.read();
      while(b!= -1)   {
       System.out.print((char)b);
       b = in.read();
      }
      in.close(); 
     }
    }
    7.try(类名 变量名 = new 类型()){
        ...
    }
     
    自动添加finally{ 变量.close();},无论是否异常,都会执行
    import java.io.*;
    class TryWithResourcesTest {
        public static void main(String ... args)
      throws IOException
     {
      String path = "c:\\aaa.txt";
      System.out.println( ReadOneLine1( path ) );
      System.out.println( ReadOneLine2( path ) );
        }
     static String ReadOneLine1(String path){
      BufferedReader br=null;
            try {
                br=new BufferedReader(new FileReader(path));
                return br.readLine();
            } catch(IOException e) {
                e.printStackTrace();
            } finally {
                if(br!=null){
        try{
         br.close();
        }catch(IOException ex){
        }
       }
            }
      return null;
     }
     static String ReadOneLine2(String path)
      throws IOException
     {
      try(BufferedReader br= new BufferedReader(new FileReader(path))){
                return br.readLine();
            }
     }
    }
    8.自定义异常类
    创建用户自定义异常时,继承Exception类或者某个子Exception类,定义属性和方法,或重载父类的方法。
    9.重抛异常及异常链接
    将异常传给调用者,让调用者感到异常,在catch或者finally语句可采用三种方式
    *将捕获的异常再次抛出:throw e;
    *重新生成异常并抛出   throw new Exception("some message");
    *重新生成并抛出一个新的异常,该异常包含了当前异常的信息
    throw new Exception("some message",e);
    可用e.getCause得到内部异常
    public class ExceptionCause {
     public static void main(String [] args) {
      try
      {
       BankATM.GetBalanceInfo( 12345L);
      }catch(Exception e) {
       System.out.println("something wrong: " + e);
       System.out.println("cause:" + e.getCause());
      }
     }
    }
    class DataHouse {
     public static void FindData( long ID)
      throws DataHouseException
     {
      if( ID>0 && ID<1000)
       System.out.println( "id: " + ID );
      else
       throw new DataHouseException("cannot find the id");
     }
    }
    class BankATM{
     public static void GetBalanceInfo( long  ID)
      throws MyAppException
     {
      try
      {
       DataHouse.FindData(ID);
      }catch (DataHouseException e) {
       throw new MyAppException("invalid id",e);
      }
     }
    }
    class DataHouseException extends Exception {
     public DataHouseException( String message ) {
      super(message);
     }
    }
    class MyAppException extends Exception {
     public MyAppException (String message){
      super (message);
     }
     public MyAppException (String message, Exception cause) {
      super(message,cause);
     }  
    }
     
     
     
  • 相关阅读:
    RAD Studio最终版合集
    cxGrid 锁定一行,让该行数据不能编辑
    跨平台打开一个URL的方法
    【转】DELPHI开始支持LINUX DOCKER
    HTTP请求的拦截
    SVG图像
    Kafka
    HBase分布式集群部署
    HBase
    Mapreduce提交YARN集群运行
  • 原文地址:https://www.cnblogs.com/SirNie/p/13347313.html
Copyright © 2011-2022 走看看