20191102Java课堂记录
1.
import javax.swing.*;
class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
try
{
k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
}
// catch ( ArithmeticException e)
// {
// System.out.println("111被0除. "+ e.getMessage());
// }
catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage());
}
}
finally
{
JOptionPane.showConfirmDialog(null,"OK");
}
}
}
2.
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");
}
}
}
运行结果:
ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException
3.
public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "11ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("22发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.err.println( "33ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
运行结果:
33ArrayIndexOutOfBoundsException/外层try-catch
4.
public 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 2
try {
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");
}
}
}
结论:
当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
5.
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");
}
}
}
结论:
finally不一定会执行
20191102Java课堂记录的更多相关文章
- Java学习笔记二--API课堂记录
JavaSE课堂记录(二) 第一节课 方法的重载:方法名相同,参数列表不同 方法的重写:方法名,参数列表相同 两同:方法名相同,参数列表相同 两小:访问权限小与等于父类,返回值类型小于等于父类(返回值 ...
- [C++讨论课] 课堂记录(一)
今天第一次参加c++讨论课,记录下了各组同学的展示的问题或者解决方法,也有一些知识点上的内容,供以后复习参考. 1.常量指针和指针常量问题 常量指针:指向常量的指针,例如const int *p = ...
- 20191014Java课堂记录
1. Java字段初始化的规律 首先执行类成员定义时指定的默认值或类的初始化块,到底执行哪一个要看哪一个“排在前面”. 其次执行类的构造函数. 类的初始化块不接收任何的参数,而且只要一创建类的对象,它 ...
- 20190925Java课堂记录(二)
1. testrandom public class test2{ public static void main(String[] args) { int[] n=new int [1001]; n ...
- 20190925Java课堂记录(一)
判断字符串是否回文 设计思想 利用递归,每次返回长度减二的字符串,最终返回结果 源程序代码 import java.util.Scanner; public class palindrome { st ...
- 20190918Java课堂记录
1. EnumTest.java public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; ...
- JavaWeb 学习009-4个页面,5条sql语句(添加、查看、修改、删除)
===========++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==+++++++++ 2016-12-3------ ...
- 机器学习笔记(三)Logistic回归模型
Logistic回归模型 1. 模型简介: 线性回归往往并不能很好地解决分类问题,所以我们引出Logistic回归算法,算法的输出值或者说预测值一直介于0和1,虽然算法的名字有“回归”二字,但实际上L ...
- Cs231n课堂内容记录-Lecture 4-Part1 反向传播及神经网络
反向传播 课程内容记录:https://zhuanlan.zhihu.com/p/21407711?refer=intelligentunit 雅克比矩阵(Jacobian matrix) 参见ht ...
随机推荐
- 【u236】火炬
Time Limit: 1 second Memory Limit: 128 MB 2008北京奥运会,你想成为四川汶川的一名火炬手,结果层层选拔,终于到了最后一关,这一关是一道很难的题:任意给定一个 ...
- Anagram——[枚举全排列]
预备知识: 1.求0—n个数全排列的算法: void print_permutation(int n,int *A,int cur){ if(cur==n){ ;i<cur;i++) cout& ...
- linux kdb 内核调试器
许多读者可能奇怪为什么内核没有建立更多高级的调试特性在里面.答案, 非常简单, 是 Linus 不相信交互式的调试器. 他担心它们会导致不好的修改, 这些修改给问题打了补丁 而不是找到问题的真正原因. ...
- .Net Core解除文件上传大小限制
一共要修改两处地方: \Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc( ...
- Java 学习笔记(4)——面向对象
现在一般的语言都支持面向对象,而java更是将其做到很过分的地步,java是强制使用面向对象的写法,简单的写一个Hello Word都必须使用面向对象,这也是当初我很反感它的一点,当然现在也是很不喜欢 ...
- C++ 动态加载 DLL 时,GetProcAddress() 返回 NULL,GetLastError() 获取错误代码为 127
1.问题现象: 采用“运行期间动态链接”自己的 dll 文件,LoadLibrary() 成功获取 dll 模块句柄,但是 GetProcAddress() 返回 NULL. 2.问题分析: 调用 G ...
- slim中返回结果加密的
//返回结果不加密 $this->get("/open]",function (Request $request, Response $response, $args) { ...
- Vijos1788 第K大 [模拟]
1.题意:给定N个数字,和一个值K,要求输出一组数据中第K大的数字,其中30%的测试点满足:n <= 100;60%的测试点满足:n <= 1000;100%的测试点满足:n <= ...
- Python之time模块和datatime模块
import time time.sleep(5) #休眠 time.time() #返回系统时间戳 utc时间秒数 time.ctime() #返回字符串时间格式,也可以传入参数转换为字符串时间ti ...
- 第二阶段:2.商业需求分析及BRD:2.产品需求池
需求获取方式 比如公司战略方面的需求 用户的反馈:投诉 建议等等 产品经理需要时刻关注竞品以及行业的发展! 需求池:各个产品经理的需求总和成一个需求池.让资源更好的利用起来.有的公司还有个“需求管理 ...