动手动脑4

import java.io.*;
public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉异常"); }}private static void throwsTest()  throws ArithmeticException,IOException { System.out.println("这只是一个测试"); // 程序处理过程假设发生异常throw new IOException(); //throw new ArithmeticException(); 
    } 
}
public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("发生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); } } 
}
CatchWho2.java
public class CatchWho2 { public static void main(String[] args) { try {try { throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); }throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("发生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); } } 
}
CheckedExceptionDemo.java
import java.io.*; public class CheckedExceptionDemo { public static void main(String[] args)  { try { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in));    //抛出受控的异常System.out.print("请输入整数: "); int input = Integer.parseInt(buf.readLine()); //有可能引发运行时异常System.out.println("input x 10 = " + (input*10)); } //以下异常处理语句块是必须的,否则无法通过编译catch(IOException e) { System.out.println("I/O错误"); } //以下异常处理语句块可以省略,不影响编译,但在运行时出错catch(NumberFormatException e) { System.out.println("输入必须为整数"); }} 
}
EmbededFinally.javapublic class EmbededFinally {public static void main(String args[]) {int result;try {System.out.println("in Level 1");try {System.out.println("in Level 2");// result=100/0;  //Level 2try {System.out.println("in Level 3");result=100/0;  //Level 3
                } catch (Exception e) {System.out.println("Level 3:" + e.getClass().toString());}finally {System.out.println("In Level 3 finally");}// result=100/0;  //Level 2
}catch (Exception e) {System.out.println("Level 2:" + e.getClass().toString());}finally {System.out.println("In Level 2 finally");}// result = 100 / 0;  //level 1
        } catch (Exception e) {System.out.println("Level 1:" + e.getClass().toString());}finally {.             System.out.println("In Level 1 finally");}}}
ExceptionLinkInRealWorld.java
/*** 自定义的异常类* @author JinXuLiang**/
class MyException extends Exception
{public MyException(String Message) {super(Message);}public MyException(String message, Throwable cause) {super(message, cause);}public MyException( Throwable cause) {super(cause);}}public class ExceptionLinkInRealWorld {public static void main( String args[] ){try {throwExceptionMethod();  //有可能抛出异常的方法调用
      }catch ( MyException e ){System.err.println( e.getMessage() );System.err.println(e.getCause().getMessage());}catch ( Exception e ){System.err.println( "Exception handled in main" );}doesNotThrowException(); //不抛出异常的方法调用
   }public static void throwExceptionMethod() throws MyException{try {System.out.println( "Method throwException" );throw new Exception("系统运行时引发的特定的异常");  // 产生了一个特定的异常
      }catch( Exception e ){System.err.println("Exception handled in method throwException" );//转换为一个自定义异常,再抛出throw new MyException("在方法执行时出现异常",e);}finally {System.err.println("Finally executed in throwException" );}// any code here would not be reached
   }public static void doesNotThrowException(){try {System.out.println( "Method doesNotThrowException" );}catch( Exception e ){System.err.println( e.toString() );}finally {System.err.println("Finally executed in doesNotThrowException" );}System.out.println("End of method doesNotThrowException" );}
}
ExplorationJDKSource.javapublic class ExplorationJDKSource {/*** @param args*/public static void main(String[] args) {System.out.println(new A());}}class A{}
OverrideThrows.javaimport java.io.*;public class OverrideThrows
{public void test()throws IOException{FileInputStream fis = new FileInputStream("a.txt");}
}
class Sub extends OverrideThrows
{//如果test方法声明抛出了比父类方法更大的异常,比如Exception//则代码将无法编译……public void test() throws FileNotFoundException{//...
    }
}+
// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack {public static void main( String args[] ){try {method1();}catch ( Exception e ) {System.err.println( e.getMessage() + "\n" );e.printStackTrace();}}public static void method1() throws Exception{method2();}public static void method2() throws Exception{method3();}public static void method3() throws Exception{throw new Exception( "Exception thrown in method3" );}
}public class SystemExitAndFinally {public static void main(String[] args){try{System.out.println("in main");throw new Exception("Exception is thrown in main");//System.exit(0);
}catch(Exception e){System.out.println(e.getMessage());System.exit(0);}finally{System.out.println("in finally");}}}
ThrowDemo.java
public class ThrowDemo { public static void main(String[] args) { 
//        try {double data = 100 / 0.0;System.out.println("浮点数除以零:" + data); 
//            if(String.valueOf(data).equals("Infinity"))
//            { 
//                System.out.println("In Here" ); 
//                throw new ArithmeticException("除零异常");
//            }
//        } 
//        catch(ArithmeticException e) { 
//            System.out.println(e); 
//        } 
    } 
}
import java.io.*;
public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉异常"); }}private static void throwsTest()  throws ArithmeticException,IOException { System.out.println("这只是一个测试"); // 程序处理过程假设发生异常throw new IOException(); //throw new ArithmeticException(); 
    } 
}

 

转载于:https://www.cnblogs.com/sljslj/p/9944151.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/250267.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javascript --- 对象原型

对象原型 参考 - MDN Javascript中的原型 在Javascript中,每一个函数都有一个特殊的属性,叫做原型 下面获取函数的原型fn.prototype function f1(){} console.log(f1.prototype) /*{constructor: f f1()__proto__:{constructor: f Object()__defineGetter__: f __defineGe…

从零认识单片机(9)

keil软件: IDE:IDE是集成开发环境,就是用来开发的完整的软件系统。 keil和mdk: keil:只能用来开发单片机 mdk:基于keil 拓展ARM的开发,主要用来开发ARM-cortex-m系列单片机的程序。 使用keil打开已有的工程项目: 1、IDE开发软件&a…

javascript --- vue2.x中原型的使用(拦截数组方法) 响应式原理(部分)

说明 在Vue2.x中,利用了对原型链的理解,巧妙的利用JavaScript中的原型链,实现了数组的pop、push、shift、unshift、reverse、sort、splice等的拦截. 你可能需要的知识 参考 - MDN 原型链 JavaScript常被描述为一种基于原型的语言(prototype-based language),每个对象拥有一…

dubbo-admin构建报错

dubbo-admin构建报错 意思是maven库里没有dubbo2.5.4-SNAPSHOT.jar这个版本的dubbo的jar包&#xff0c;把dubbo-admin项目的pom.xml的   <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${proje…

javascript --- 手写Promise、快排、冒泡、单例模式+观察者模式

手写promise 一种异步的解决方案, 参考 Promise代码基本结构 function Promise(executor){this.state pending;this.value undefined;this.reason undefined;function resolve(){}function reject(){} } module.exports Promisestate保存的是当前的状态,在Promise状态发…

PyCharm 通过Github和Git上管理代码

1.Pycharm中设置如图: 2.配置Git,通过网页 https://www.git-scm.com/download/win 下载 3. 转载于:https://www.cnblogs.com/0909/p/9956406.html

【BZOJ】2395: [Balkan 2011]Timeismoney

题解 最小乘积生成树&#xff01; 我们把&#xff0c;x的总和和y的总和作为x坐标和y左边&#xff0c;画在坐标系上 我们选择两个初始点&#xff0c;一个是最靠近y轴的A&#xff0c;也就是x总和最小&#xff0c;一个是最靠近x轴的B&#xff0c;也就是y总和最小 连接两条直线&…

http --- http与https相关概念小结

网络协议 参考 HTTP的特性 HTTP协议构建于TCP/IP协议之上,是一个应用层协议,默认端口是80HTTP是无连接无状态的 HTTP报文 请求报文 HTTP协议是以ASCII码传输,建立在 TCP/IP 协议之上的应用层规范。规范把HTTP请求分为三个部分:状态行、请求头、消息主体。 <method>…

Spring AOP注解方式实现

简介 上文已经提到了Spring AOP的概念以及简单的静态代理、动态代理简单示例&#xff0c;链接地址&#xff1a;https://www.cnblogs.com/chenzhaoren/p/9959596.html 本文将介绍Spring AOP的常用注解以及注解形式实现动态代理的简单示例。 常用注解 aspect&#xff1a;定义切面…

享元模式-Flyweight(Java实现)

享元模式-Flyweight 享元模式的主要目的是实现对象的共享,即共享池,当系统中对象多的时候可以减少内存的开销,通常与工厂模式一起使用。 本文中的例子如下: 使用享元模式: 小明想看编程技术的书, 就到家里的书架上拿, 如果有就直接看, 没有就去买一本, 回家看. 看完了就放到家里…

算法 --- 回溯法

回溯法 参考 - 剑指Offer 回溯法可以看成蛮力法的升级版,它从解决问题每一步的所有可能选项里系统地选择出一个可行的解决方案. 回溯法解决的问题的特性: 可以形象地用树状结构表示: 节点: 算法中的每一个步骤节点之间的连接线: 每个步骤中的选项,通过每一天连接线,可以到达…

013.Zabbix的Items(监控项)

一 Items简介 Items是从主机里面获取的所有数据&#xff0c;可以配置获取监控数据的方式、取值的数据类型、获取数值的间隔、历史数据保存时间、趋势数据保存时间、监控key的分组等。通常情况下item由key参数组成&#xff0c;如监控项中需要获取cpu信息&#xff0c;则需要一个对…

Cookie 和 Session的区别

pass 下次再写转载于:https://www.cnblogs.com/nieliangcai/p/9073520.html

算法 --- 记一道面试dp算法题

题目: 给定一个数组(长度大于1),如下 let a [1,4,3,4,5] // 长度不确定,数值为整数要求写一个函数,返回该数组中,除本身数字之外其他元素的成积.即返回如下: // 过程[4*3*4*5, 1*3*4*5, 1*4*4*5, 1*4*3*5, 1*4*3*4] // 结果[240, 60, 80, 60, 48]题目要求不使用除法,且时间…

编码

一、什么是编码&#xff1f;首先&#xff0c;我们从一段信息即消息说起&#xff0c;消息以人类可以理解、易懂的表示存在。我打算将这种表示称为“明文”&#xff08;plain text&#xff09;。对于说英语的人&#xff0c;纸张上打印的或屏幕上显示的英文单词都算作明文。其次&a…

ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(10)之素材管理

源码下载地址&#xff1a;http://www.yealuo.com/Sccnn/Detail?KeyValuec891ffae-7441-4afb-9a75-c5fe000e3d1c 素材管理模块也是我们这个项目的核心模块&#xff0c;里面的增删查改都跟文章管理模块相同或者相似&#xff0c;唯一不同点可能是对附件的上传处理&#xff0c;但…

javascript --- [express+ vue2.x + elementUI]登陆的流程梳理

说明 涉及到以下知识点: 登陆的具体流程express、vue2.x、elementUI、axios、jwt、assert 登陆方面的API使用中间件的使用前后端通过http状态码,进行响应的操作(这里主要是401)密码验证(bcrypt的hashSync方法对明文密码进行加密,compareSync方法对加密的密码进行验证)token的…

设计模式---装饰模式

今天学习了装饰模式&#xff0c;做个笔记。。装饰模式的基础概念可以参考&#xff1a;https://blog.csdn.net/cjjky/article/details/7478788 这里&#xff0c;就举个简单例子 孙悟空有72变&#xff0c;但是它平时是猴子&#xff0c;遇到情况下&#xff0c;它可以变成蝴蝶等等 …

springMvc 注解@JsonFormat 日期格式化

1&#xff1a;一定要加入依赖,否则不生效&#xff1a; <!--日期格式化依赖--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version>&…

Git很简单--图解攻略

Git Git 是目前世界上最先进的分布式版本控制系统&#xff08;没有之一&#xff09;作用 源代码管理为什么要进行源代码管理? 方便多人协同开发方便版本控制Git管理源代码特点 1.Git是分布式管理.服务器和客户端都有版本控制能力,都能进行代码的提交、合并、. 2.Git会在根…