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. 01-RocketMQ介绍

    一.MQ介绍 1.什么是MQ?为什么要用MQ? MQ:MessageQueue,消息队列. 队列,是一种FIFO 先进先出的数据结构.消息由生产者发送到MQ进行排队,然后按原来的顺序交由消息的消费者进 ...

  2. Python模板引擎Jinja2使用简介

    原文链接 背景 最近在项目开发中,需要针对 Jenkins 项目进行配置,Jenkins 的 job 配置采用的是 xml,在维护配置模板的过程中就遇到了问题,因为逐步发现配置灵活性超出了字符串的范畴 ...

  3. PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation

    摘要 点云是一种重要的几何数据结构类型.由于其不规则的格式,大多数研究人员将此类数据转化为常规的三维体素网格或图像集合.然而,这使数据变得不必要的庞大,并导致问题.在本文中,我们设计了一种新型的直接处 ...

  4. LGP5312题解

    压 位 T r i e 入 门 练 习 题(确信) 题意很清楚( 让我们先来想一想,如果没有排序操作的话,这道题应该怎么做. 我们维护一个 \(x\) 表示从开始到现在一共异或上了 \(x\),在序列 ...

  5. VuePress 博客之 SEO 优化(一) sitemap 与搜索引擎收录

    前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 本篇讲讲 ...

  6. CentOS7.5环境下安装配置GitLab

    1. 安装依赖软件 yum -y install policycoreutils openssh-server openssh-clients postfix 2.设置postfix开机自启,并启动, ...

  7. 最长公共子串(DP)

    DP基础_最长公共子串 Description 两个序列的最长公共子串,这个子串要求在序列中是连续的.如:"bab"和"caba" (可以看出来最长公共子串是& ...

  8. unable to resolve class XXX

    > Task :HelloWorld_Web:compileGroovy startup failed: E:\GradleDemoManyModules\ExampleHelloWorld\H ...

  9. keybd_event 在F按键系列不起作用的解决办法

    最近给公司做自动化测试工具,主要用到的功能是模拟鼠标键盘录制回放.一切都很完美了,但在客户大机系统上使用的时候,发现F1-F24系列按键无法正确使用,查了很多资料,主要有2个方面的原因: 1.一些游戏 ...

  10. @Bean和@Componet区别

    无意在两个类上看到了这两个注解,一个使用了@Bean配合@Configuration,一个使用了@Componet.依稀记得这两个注解都是实现以前在xml中<bean xxx/>的功能,但 ...