java 批量处理 示例_Java中异常处理的示例

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 批量处理 示例

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

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

相关文章

hdu 1465 不容易系列之一

http://acm.hdu.edu.cn/showproblem.php?pid1465 今天立神和我们讲了错排,才知道错排原来很简单,从第n个推起: 当n个编号元素放在n个编号位置,元素编号与位置编号各不对应的方法数用M(n)表示,那么M(n-1)就表示n-1个编号元素放在n-1个编号位置…

第十四章 网络编程

第十四章 网络编程 本章首先概述Python标准库中的一些网络模块。然后讨论SocketServer和相关的类,并介绍同时处理多个连接的各种方法。最后,简单地说一说Twisted,这是一个使用Python编写网络程序的框架,功能丰富而成熟。 几个网…

c语言输出11258循环,c/c++内存机制(一)(转)

一:C语言中的内存机制在C语言中,内存主要分为如下5个存储区:(1)栈(Stack):位于函数内的局部变量(包括函数实参),由编译器负责分配释放,函数结束,栈变量失效。(2)堆(Heap):由程序员用…

【神经网络八股扩展】:数据增强

课程来源:人工智能实践:Tensorflow笔记2 文章目录前言TensorFlow2数据增强函数数据增强网络八股代码:总结前言 本讲目标:数据增强,增大数据量 关于我们为何要使用数据增强以及常用的几种数据增强的手法,可以看看下面的文章&#…

C++:从C继承的标准库

C从C继承了的标准库 &#xff0c; 这就意味着 C 中 可以使用的标准库函数 在C 中都可以使用 &#xff0c; 但是需要注意的是 &#xff0c; 这些标准库函数在C中不再以 <xxx.h> 命名 &#xff0c; 而是变成了 <cxxx> 。 例如 &#xff1a; 在C中操作字符串的…

分享WCF聊天程序--WCFChat

无意中在一个国外的站点下到了一个利用WCF实现聊天的程序&#xff0c;作者是&#xff1a;Nikola Paljetak。研究了一下&#xff0c;自己做了测试和部分修改&#xff0c;感觉还不错&#xff0c;分享给大家。先来看下运行效果&#xff1a;开启服务&#xff1a;客户端程序&#xf…

c# uri.host_C#| 具有示例的Uri.Equality()运算符

c# uri.hostUri.Equality()运算符 (Uri.Equality() Operator) Uri.Equality() Operator is overloaded which is used to compare two Uri objects. It returns true if two Uri objects contain the same Uri otherwise it returns false. Uri.Equality()运算符已重载&#xf…

第六章至第九章的单元测试

1,‌助剂与纤维作用力大于纤维分子之间的作用力,则该助剂最好用作() 纤维增塑膨化剂。 2,助剂扩散速率快,优先占领纤维上的染座,但助剂与纤维之间作用力小于染料与纤维之间作用力,该助剂可以作为() 匀染剂。 3,助剂占领纤维上的染座,但助剂与纤维之间作用力大于染…

【神经网络扩展】:断点续训和参数提取

课程来源&#xff1a;人工智能实践:Tensorflow笔记2 文章目录前言断点续训主要步骤参数提取主要步骤总结前言 本讲目标:断点续训&#xff0c;存取最优模型&#xff1b;保存可训练参数至文本 断点续训主要步骤 读取模型&#xff1a; 先定义出存放模型的路径和文件名&#xff0…

开发DBA(APPLICATION DBA)的重要性

开发DBA是干什么的&#xff1f; 1. 审核开发人员写的SQL&#xff0c;并且纠正存在性能问题的SQL ---非常重要 2. 编写复杂业务逻辑SQL&#xff0c;因为复杂业务逻辑SQL开发人员写出的SQL基本上都是有性能问题的&#xff0c;与其让开发人员写&#xff0c;不如DBA自己写。---非常…

javascript和var之间的区别?

You can define your variables in JavaScript using two keywords - the let keyword and the var keyword. The var keyword is the oldest way of defining and declaring variables in JavaScript whereas the let is fairly new and was introduced by ES15. 您可以使用两…

小米手环6NFC安装太空人表盘

以前看我室友峰哥、班长都有手环&#xff0c;一直想买个手环&#xff0c;不舍得&#xff0c;然后今年除夕的时候降价&#xff0c;一狠心&#xff0c;入手了&#xff0c;配上除夕的打年兽活动还有看春晚京东敲鼓领的红包和这几年攒下来的京东豆豆&#xff0c;原价279的小米手环6…

计算机二级c语言题库缩印,计算机二级C语言上机题库(可缩印做考试小抄资料)...

小抄,答案,形成性考核册,形成性考核册答案,参考答案,小抄资料,考试资料,考试笔记第一套1.程序填空程序通过定义学生结构体数组&#xff0c;存储了若干个学生的学号、姓名和三门课的成绩。函数fun 的功能是将存放学生数据的结构体数组&#xff0c;按照姓名的字典序(从小到大排序…

为什么两层3*3卷积核效果比1层5*5卷积核效果要好?

目录1、感受野2、2层3 * 3卷积与1层5 * 5卷积3、2层3 * 3卷积与1层5 * 5卷积的计算量比较4、2层3 * 3卷积与1层5 * 5卷积的非线性比较5、2层3 * 3卷积与1层5 * 5卷积的参数量比较1、感受野 感受野&#xff1a;卷积神经网络各输出特征像素点&#xff0c;在原始图片映射区域大小。…

算法正确性和复杂度分析

算法正确性——循环不变式 算法复杂度的计算 方法一 代换法 —局部代换 这里直接对n变量进行代换 —替换成对数或者指数的情形 n 2^m —整体代换 这里直接对递推项进行代换 —替换成内部递推下标的形式 T(2^n) S(n) 方法二 递归树法 —用实例说明 —分析每一层的内容 —除了…

第十五章 Python和Web

第十五章 Python和Web 本章讨论Python Web编程的一些方面。 三个重要的主题&#xff1a;屏幕抓取、CGI和mod_python。 屏幕抓取 屏幕抓取是通过程序下载网页并从中提取信息的过程。 下载数据并对其进行分析。 从Python Job Board&#xff08;http://python.org/jobs&#x…

array_chunk_PHP array_chunk()函数与示例

array_chunkPHP array_chunk()函数 (PHP array_chunk() Function) array_chunk() function is an array function, it is used to split a given array in number of array (chunks of arrays). array_chunk()函数是一个数组函数&#xff0c;用于将给定数组拆分为多个数组(数组…

raise

raise - Change a windows position in the stacking order button .b -text "Hi there!"pack [frame .f -background blue]pack [label .f.l1 -text "This is above"]pack .b -in .fpack [label .f.l2 -text "This is below"]raise .b转载于:ht…

c语言输出最大素数,for语句计算输出10000以内最大素数怎么搞最简单??各位大神们...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include #include int* pt NULL; // primes_tableint pt_size 0; // primes_table 数量大小int init_primes_table(void){FILE* pFile;pFile fopen("primes_table.bin", "rb");if (pFile NULL) {fputs(&q…

【数据结构基础笔记】【图】

代码参考《妙趣横生的算法.C语言实现》 文章目录前言1、图的概念2、图的存储形式1、邻接矩阵&#xff1a;2、邻接表3、代码定义邻接表3、图的创建4、深度优先搜索DFS5、广度优先搜索BFS6、实例分析前言 本章总结&#xff1a;图的概念、图的存储形式、邻接表定义、图的创建、图…