java 批量处理 示例

Here, we will analyse some exception handling codes, to better understand the concepts.

在这里,我们将分析一些异常处理代码 ,以更好地理解这些概念。

Try to find the errors in the following code, if any

尝试在以下代码中查找错误(如果有)

Code 1:

代码1:

public class prog {public static void main(String arg[]) {try {int a = 10, b = 0;
int c = a / b;
} catch (RuntimeException e) {System.out.println(e.getMessage());
} catch (ArithmeticException e) {System.out.println(e.getMessage());
}
}
}

Output

输出量

/prog.java:8: error: exception ArithmeticException has already been caught} catch (ArithmeticException e) {^
1 error

Explanation:

说明:

When using multiple catch blocks, we have to make sure that the exceptions are caught in the reverse order of inheritance of the exceptions. This means that most specific exceptions must be caught before the most general exceptions. ArithmeticException must be caught before the RuntimeException.

当使用多个catch块时,我们必须确保以与异常继承相反的顺序捕获异常。 这意味着必须在最一般的异常之前捕获最具体的异常。 必须在RuntimeException之前捕获ArithmeticException



Code 2:

代码2:

public class prog {public static void main(String arg[]) {try {throw new Integer(25);
} catch (Exception e) {System.out.println(e.getMessage());
}
}
}

Output

输出量

/prog.java:4: error: incompatible types: Integer cannot be converted to Throwablethrow new Integer(25);^
Note: /prog.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

Explanation:

说明:

We can only throw objects of Throwable class or classes that inherit this class. Integer class does not extend the Throwable class and thus, we cannot throw objects of Integer class. Similarly the statement "throw 25" is erroneous. Unlike C++, where we can throw objects, in Java we can only throw those objects that inherit the Throwable class.

我们只能抛出Throwable类或继承该类的类的对象。 Integer类不会扩展Throwable类,因此,我们不能抛出Integer类的对象。 类似地, “ throw 25”的陈述是错误的 。 与C ++可以抛出对象不同,在Java中,我们只能抛出那些继承Throwable类的对象。



Code 3:

代码3:

public class prog {public static void main(String arg[]) {err ob1 = new err();
ob1.func();
}
}
class err {void func() {try {System.out.println("Inside try");
} finally {System.out.println("Inside finally");
}
}
}

Output

输出量

Inside try
Inside finally

Explanation:

说明:

It is not necessary that we attach a catch block to a try block. Try statement can be associated with a finally statement directly.

不必将catch块附加到try块。 try语句可以直接与finally语句关联。



Code 4:

代码4:

import java.io.*;
public class prog {public static void main(String arg[]) {err ob1 = new err();
ob1.func();
}
}
class err {void func() throws IOException {try {System.out.println("Inside try");
} catch (Exception e) {System.out.println("Inside catch");
} finally {System.out.println("Inside finally");
}
}
}

Output

输出量

/prog.java:6: error: unreported exception IOException; must be caught or declared to be thrownob1.func();^
1 error

Explanation:

说明:

If a function is said to throw any checked exception, then that checked exception must be caught by the caller. The statement 'void func() throws IOException' clearly states that the function can throw an IOException that must be handled by the caller. The error can be corrected by enclosing the 'ob1.func()' statement in a try-catch block.

如果说一个函数抛出了任何已检查的异常,则调用者必须捕获该已检查的异常。 声明“ void func()throws IOException ”清楚地表明该函数可以抛出IOException,必须由调用者处理。 可以通过在try-catch块中包含' ob1.func() '语句来纠正该错误。



Code 5:

代码5:

import java.io.*;
public class prog {public static void main(String arg[]) {err ob1 = new err();
ob1.func();
}
}
class err {void func() throws RuntimeException {try {System.out.println("Inside try");
try {int[] a = new int[10];
int c = 10;
a[c] = 0;
}
} catch (Exception e) {System.out.println("Inside catch");
}
}
}

Output

输出量

/prog.java:14: error: 'try' without 'catch', 'finally' or resource declarationstry {^
1 error

Explanation:

说明:

Every try block must have an associated catch or finally block. In the code, the inner try block does not have an associated catch or finally block.

每个try块必须具有关联的catch或finally块。 在代码中,内部try块没有关联的catch或finally块。



翻译自: https://www.includehelp.com/java/examples-on-exceptional-handing-in-java.aspx

java 批量处理 示例

java 批量处理 示例_Java中异常处理的示例相关推荐

  1. java 批量写入文件_Java批量写入文件和下载图片的示例代码

    很久没有在WhitMe上写日记了,因为觉着在App上写私密日记的话肯定是不安全的,但是想把日记存下来.,然后看到有导出日记的功能,就把日记导出了(还好可以直接导出,不然就麻烦点).导出的是一个html ...

  2. java批量下载图片_Java批量写入文件和下载图片的示例代码

    很久没有在WhitMe上写日记了,因为觉着在App上写私密日记的话肯定是不安全的,但是想把日记存下来.,然后看到有导出日记的功能,就把日记导出了(还好可以直接导出,不然就麻烦点).导出的是一个html ...

  3. java中get接口示例_Java即时类| 带示例的get()方法

    java中get接口示例 即时类的get()方法 (Instant Class get() method) get() method is available in java.time package ...

  4. java中get接口示例_Java LocalDateTime类| 带示例的get()方法

    java中get接口示例 LocalDateTime类的get()方法 (LocalDateTime Class get() method) get() method is available in ...

  5. java组合与继承始示例_Java 8特性与示例

    java组合与继承始示例 Java 8 was released on 18th March 2014, so it's high time to look into Java 8 Features. ...

  6. java批量处理数据_Java批量处理数据

    要求:共1000条数据,第一次批量插入100条,第二次批量插入101到200条,依次插入数据: 实现方式这里选择了两种常用的方式,都是使用List操作: 第一种实现思路如下: <1> 原先 ...

  7. java可以多重继承吗_Java中的多重继承与组合vs继承

    java可以多重继承吗 有时我写了几篇有关Java继承,接口和组成的文章. 在这篇文章中,我们将研究多重继承,然后了解组成优于继承的好处. Java中的多重继承 多重继承是创建具有多个超类的单个类的能 ...

  8. java构造器调用构造器_java中构造器内部调用构造器实例详解

    可能为一个类写了多个构造器,有时可能想在一个构造器里面调用另外一个构造器,为了减少代码的重复,可用this关键字做到这一点. public class Flower { private String ...

  9. java设计模式工厂模式_Java中的工厂设计模式

    java设计模式工厂模式 Welcome to the Factory Design Pattern in Java tutorial. Factory Pattern is one of the C ...

最新文章

  1. leangoo怎么导入导出,归档和删除看板?
  2. 宏基因组扩增子3统计绘图:中文首发,最详系,零基础(箱线图、散点图、热图、曼哈顿图、火山图、韦恩图、三元图、网络图)
  3. 目标检测--Wide-Residual-Inception Networks for Real-time Object Detection
  4. 记录下最近写前端的一些小技巧
  5. 动态DNS——本质上是IP变化,将任意变换的IP地址绑定给一个固定的二级域名。不管这个线路的IP地址怎样变化,因特网用户还是可以使用这个固定的域名 这样看的话,p2p可以用哇...
  6. ubuntu 设置root启动
  7. 揭秘!疫情下的阿里员工如何上班?
  8. 带哨兵节点的链_关于链表中哨兵结点问题的深入剖析
  9. Oracle数据库体系结构
  10. 6月26号.NET面试题(程序题部分)只要做懂这3道题肯定能脱离菜鸟称号!
  11. WebBrowser设置打印页眉页眉和页边距
  12. 5G iPhone SE起售价399美元 今年有望出货3000万部
  13. web开发移动端准备工作
  14. php min命令,php min函数怎么用 - min
  15. 解决Win7下JMF读取摄像头错误
  16. mysql可以用表情符号_让MySQL支持emoji表情符号存储
  17. 阿里巴巴矢量图库,图标导入的简单使用
  18. NOIP2010导弹拦截
  19. 使用这些方式让你的ipad拥有更长的使用寿命
  20. iOS年月日、时分秒选择器

热门文章

  1. C++ 在屏幕上用星号打印菱形
  2. docker安装clickhouse_clickhouse ----入门
  3. c语言成绩转换绩点,如何将平时成绩转化为GPA成绩?
  4. Angularjs基础(三)
  5. vue class绑定方式
  6. js-原始类型和声明变量
  7. 上下div高度动态自适应--另类处理方案
  8. js笔记(六)事件、正则
  9. layui 父页面弹框中获取子页面的内容
  10. 第7章 输入/输出系统