java中抛出throw关键字是怎么用的? 举例?
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关键字是怎么用的? 举例?的更多相关文章
- java中异常的抛出:throw throws
java中异常的抛出:throw throws Java中的异常抛出 语法: public class ExceptionTest{ public void 方法名(参数列表) throws 异常列表 ...
- Java异常处理-----抛出处理
抛出处理 定义一个功能,进行除法运算例如(div(int x,int y))如果除数为0,进行处理. 功能内部不想处理,或者处理不了.就抛出使用throw new Exception("除数 ...
- Java中的50个关键字
form:http://blog.csdn.net/luoweifu/article/details/6776240 Java中的50个关键字 关键字也称为保留字,是指java语言中规定了特定含义的标 ...
- 从constructor中抛出exception后,constructor会返回null吗?
刚才琢磨这个问题主要是在想,如果constructor抛出了exception,那么返回的object是什么一个情况呢?如果我这个object中有一些关键的资源没有初始化,比如说Database co ...
- java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误
/** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...
- Java异常抛出
如果要在一段代码中抛出一个已检查的异常,有两个选择: 使用try-catch块处理已检查的异常. 在方法/构造函数声明中用throws子句指定. 语法 throws子句的一般语法是: 1 2 3 &l ...
- Java中的两个关键字——super、this
Java中的两个关键字——super.this 神话丿小王子的博客主页 一.super super 是java中方的一个关键字,用它可以引用父类中的成员: super可用于访问父类中定义的属性 sup ...
- Java中过滤出字母、数字和中文的正则表达式
1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A-Za-z)] (2)过滤出数字的正则表达式 [^(0-9)] (3)过滤出中文的正则表达式 [^(\\u4e0 ...
- ClassLoader Java中类加载出现在哪个阶段,编译期和运行期? 类加载和类装载是一样的吗
1.ClassLoader Java中类加载出现在哪个阶段,编译期和运行期? 类加载和类装载是一样的吗? :当然是运行期间啊,我自己有个理解误区,改正后如下:编译期间编译器是不去加载类的,只负责编译而 ...
随机推荐
- SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)
SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...
- H3C三层交换机之IRF虚拟化技术详解及配置
一.IRF是什么? 目前,网络中主要存在两种结构的通信设备,固定盒式设备和模块框式分布式设备. 固定盒式设备成本低廉,但没有高可用性支持:模块框式分布式设备具有高可用性.高性能.高端口密度的优点,但投 ...
- jdbc model 代码示例
package com.gylhaut.model; import java.util.Date; public class Goddess { @Override public String toS ...
- Gin 09 HTTP 重定向
gin http 重定向有两种方法,重写url 和 不重写的跳转.两种方法,gin 通过两个内置方法实现: demo package main import ( "github.com/gi ...
- MySQL CREATE TABLE 简单设计模板交流
推荐用 MySQL 8.0 (2018/4/19 发布, 开发者说同比 5.7 快 2 倍) 或同类型以上版本. CREATE TABLE TEMPLATE CREATE TABLE [table ...
- java-计算机
计算机 硬件 装机:CPU 内存 主板 IO设备(input output) 冯诺依曼体系结构 CPU读取数据在运算器中运算传输到存储器,控制器控制输出结果. 软件
- Docker——run指令中-it与-d的关系
建立相关的测试容器 #1.只有-d [root@iZwz908j8pbqd86doyrez5Z test]# docker run -d -p 8081:8080 tomcat:9.0 #2.只有-i ...
- 关于alertmanager报No private IP address found, and explicit IP not provided
./alertmanager --config.file=alertmanager.yml level=info ts=2021-11-22T05:53:11.195Z caller=main.go: ...
- sqlmap的常用tamper脚本
sqlmap下的tamper目录存放绕过WAF脚本 使用方法 --tamper 脚本名称,脚本名称 多个tamper脚本之间用空格隔开 apostrophemask.py 用utf8代替引号 equa ...
- 文字图片在wps中清晰化方法
在wps中双击图片出属性,然后再选择文字增强.选择对比增加即可.