java中拋出throw關(guān)鍵字是怎么用的

拋出throw關(guān)鍵字  

馬克-to-win:我們先說5/0的原理,當(dāng)程序運(yùn)行到5/0的時(shí)候,java系統(tǒng)JVM會(huì)在后臺(tái)new出一個(gè)除0異常實(shí)例,之后把這個(gè)實(shí)例傳入catch塊兒供開發(fā)者使用。馬克-to-win:而這里throw new Exception();是開發(fā)者自己主動(dòng)new出一個(gè)異常實(shí)例,之后把這個(gè)實(shí)例傳入catch塊兒供開發(fā)者自己使用。馬克-to-win:對(duì)于catch來講,不管誰拋的,處理起來都一樣。
馬克-to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
(新手必須忽略)意義是什么?見后面的sun的例子(1.5.4_a):if(url==null) throw new sqlException見例:1.5.4,這樣就可以做到,有經(jīng)驗(yàn)的人(這里是sun公司),預(yù)感到大家都易犯url==null這樣的毛病(你開始不知道),于是他就throw new sqlException,(但是在sun公司寫那段代碼時(shí),他又不能處理,因?yàn)檫壿嬌?,就?yīng)該是你后來者的任務(wù)或說義務(wù),舉一個(gè)例子,爺爺規(guī)定遺產(chǎn)只能干教育,具體是生物還是物理或是數(shù)學(xué)他并不管,這里就是你必須管,但怎么管,怎么catch,你來做定奪,前人無法替你做決定)逼著你這個(gè)新手,必須 catch這樣的毛病,否則你的程序會(huì)崩潰。提醒你了,你不處理都不行。



例:1.5.1-本章源碼

public class Test {
    public static void main(String[] args)  {
        int mark_to_win = 0;
        int c;
        if (mark_to_win == 0) throw new ArithmeticException("divide by 0");
        else c=8/mark_to_win;  
        System.out.println("馬克-to-win:優(yōu)雅結(jié)束");
    }
}

輸出結(jié)果:

Exception in thread "main" java.lang.ArithmeticException: divide by 0
    at Test.main(Test.java:5)




例:1.5.2-本章源碼
public class Test {
    public static void main(String[] args)  {
        int mark_to_win = 0;
        int c;
        if (mark_to_win == 0)  c=8/mark_to_win;
        else c=8/mark_to_win; 
        System.out.println("馬克-to-win:優(yōu)雅結(jié)束");
    }
}

輸出結(jié)果:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Test.main(Test.java:5)

馬克-to-win:通過觀察,我們發(fā)現(xiàn)上面兩個(gè)例子最后報(bào)的異常的地方是一樣的!異常的效果也是等價(jià)的!馬克-to-win:如上面我們的講的,只不過一個(gè)是JVM系統(tǒng)拋出的,一個(gè)是我們自己主動(dòng)拋出的。馬克-to-win:所以為了不讓系統(tǒng)崩潰,我們需要像原來一樣捕獲一下異常就可以了。

例:1.5.3-本章源碼

public class Test {
    public static void main(String[] args)  {
        int mark_to_win = 0;
        int c;
        try{
            if (mark_to_win == 0) throw new ArithmeticException("divide by 0");
            else c=8/mark_to_win;           
        }catch(ArithmeticException a)
        {
            System.out.println(a);
        }
        System.out.println("馬克-to-win:優(yōu)雅結(jié)束");
     
    }
}

輸出結(jié)果:

java.lang.ArithmeticException: divide by 0
馬克-to-win:優(yōu)雅結(jié)束




請大家參見下面sun公司的java.sql.DriverManager.getConnection的源代碼。在我們的代碼中, 我們也需要處理SQLException

例:1.5.4_a:

    private static Connection getConnection(
        String url, java.util.Properties info, Class<?> caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        synchronized(DriverManager.class) {
            // synchronize loading of the correct classloader.
            if (callerCL == null) {
                callerCL = Thread.currentThread().getContextClassLoader();
            }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }





例:1.5.4(參考視頻講課用,新手必須忽略)
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) throws
            ClassNotFoundException {
        java.sql.Connection connection = null;
        java.sql.Statement statement = null;
        java.sql.ResultSet resultSet = null;
        Class.forName("com.mysql.jdbc.Driver");
        try {
            connection = java.sql.DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "1234");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from login");
            while (resultSet.next()) {
                System.out.println(resultSet.getString("id") + "--"
                        + resultSet.getString("name"));
            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}