5.抛出throw关键字

马克-to-win:我们先说5/0的原理,当程序运行到5/0的时候,java系统JVM会在后台new出一个除0异常实例,之后把这个实例传入catch块儿供开发者使用。马克-to-win:而这里throw new Exception();是开发者自己主动new出一个异常实例,之后把这个实例传入catch块儿供开发者自己使用。马克-to-win:对于catch来讲,不管谁抛的,处理起来都一样。

(新手必须忽略)意义是什么?见后面的sun的例子(1.5.4_a):if(url==null) throw new sqlException见例:1.5.4,这样就可以做到,有经验的人(这里是sun公司),预感到大家都易犯url==null这样的毛病(你开始不知道),于是他就throw new sqlException,(但是在sun公司写那段代码时,他又不能处理,因为逻辑上,就应该是你后来者的任务或说义务,举一个例子,爷爷规定遗产只能干教育,具体是生物还是物理或是数学他并不管,这里就是你必须管,但怎么管,怎么catch,你来做定夺,前人无法替你做决定)逼着你这个新手,必须catch这样的毛病,否则你的程序会崩溃。提醒你了,你不处理都不行。

例: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:优雅结束");
    }
}

输出结果:

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:优雅结束");
    }
}

输出结果:

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

马 克-to-win:通过观察,我们发现上面两个例子最后报的异常的地方是一样的!异常的效果也是等价的!马克-to-win:如上面我们的讲的,只不过一 个是JVM系统抛出的,一个是我们自己主动抛出的。马克-to-win:所以为了不让系统崩溃,我们需要像原来一样捕获一下异常就可以了。

例: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:优雅结束");
       
    }
}

输出结果:

java.lang.ArithmeticException: divide by 0
马克-to-win:优雅结束

请大家参见下面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();
        }
    }
}

更多请看下节:https://blog.csdn.net/qq_44639795/article/details/103109346

java中抛出throw关键字是怎么用的? 举例?的更多相关文章

  1. java中异常的抛出:throw throws

    java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...

  2. Java异常处理-----抛出处理

    抛出处理 定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理. 功能内部不想处理,或者处理不了.就抛出使用throw new Exception("除数 ...

  3. Java中的50个关键字

    form:http://blog.csdn.net/luoweifu/article/details/6776240 Java中的50个关键字 关键字也称为保留字,是指java语言中规定了特定含义的标 ...

  4. 从constructor中抛出exception后,constructor会返回null吗?

    刚才琢磨这个问题主要是在想,如果constructor抛出了exception,那么返回的object是什么一个情况呢?如果我这个object中有一些关键的资源没有初始化,比如说Database co ...

  5. java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误

    /** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...

  6. Java异常抛出

    如果要在一段代码中抛出一个已检查的异常,有两个选择: 使用try-catch块处理已检查的异常. 在方法/构造函数声明中用throws子句指定. 语法 throws子句的一般语法是: 1 2 3 &l ...

  7. Java中的两个关键字——super、this

    Java中的两个关键字——super.this 神话丿小王子的博客主页 一.super super 是java中方的一个关键字,用它可以引用父类中的成员: super可用于访问父类中定义的属性 sup ...

  8. Java中过滤出字母、数字和中文的正则表达式

    1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A-Za-z)] (2)过滤出数字的正则表达式 [^(0-9)] (3)过滤出中文的正则表达式 [^(\\u4e0 ...

  9. ClassLoader Java中类加载出现在哪个阶段,编译期和运行期? 类加载和类装载是一样的吗

    1.ClassLoader Java中类加载出现在哪个阶段,编译期和运行期? 类加载和类装载是一样的吗? :当然是运行期间啊,我自己有个理解误区,改正后如下:编译期间编译器是不去加载类的,只负责编译而 ...

随机推荐

  1. petite-vue源码剖析-属性绑定`v-bind`的工作原理

    关于指令(directive) 属性绑定.事件绑定和v-modal底层都是通过指令(directive)实现的,那么什么是指令呢?我们一起看看Directive的定义吧. //文件 ./src/dir ...

  2. JZ-027-字符串的排列

    字符串的排列 题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则按字典序打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和c ...

  3. springboot-jta-atomikos多数据源事务管理

    背景 我们平时在用springboot开发时,要使用事务,只需要在方法上添加@Transaction注解即可,但这种方式只适用单数据源,在多数据源下就不再适用: 比如在多数据源下,我们在一个方法里执行 ...

  4. 微服务7:通信之RPC

    ★微服务系列 微服务1:微服务及其演进史 微服务2:微服务全景架构 微服务3:微服务拆分策略 微服务4:服务注册与发现 微服务5:服务注册与发现(实践篇) 微服务6:通信之网关 微服务7:通信之RPC ...

  5. CSS 选择器学习总结

    1.id 选择器 #idname{color:red;} 2.class选择器 .classname{} 3.标签选择器 div{} 4.通配符选择器 *{} 5. 属性选择器 [id]{ } 5.选 ...

  6. 一步一步迁移ASP.NET Core 6.0-Part1

    .NET 6 发布后,我们现有的应用会逐步升级到这个版本,首当其冲的是原因的ASP.NET Core的工程,如果一步一步升级到ASP.NET Core 6.0 本文简单整理一下升级ASP.NET Co ...

  7. CF回顾《二分类》

    cf Educational Codeforces Round 115 (Rated for Div. 2) C题 类型:二分查找. 中文题目: C.删除两项内容 Monocarp有一个由n个整数组成 ...

  8. php——新闻项目改写

    主要思路:遵守java开发规范,保持接口一致性 如何保持接口的一致性: (1).url的一致性:使用@RequestingMapping注解 (2).参数的一致性: 使用@ReuqestParam注解 ...

  9. Mybatis中Log4j日志的使用

    参考资料: (1). 百度百科:https://baike.baidu.com/item/log4j/480673?fr=aladdin (2). B站狂神的视频:https://www.bilibi ...

  10. ssh-ssl编译安装

    升级前准备 #下载所需依赖包#yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel pam-d ...