首页  >  文章  >  Java  >  Java 中的 throw 关键字

Java 中的 throw 关键字

WBOY
WBOY原创
2024-08-30 15:21:54243浏览

这个 Throw 关键字主要用于异常处理的概念。顾名思义, throw 关键字的作用是从程序中向编译器抛出异常。异常简介是编译器在代码运行期间出现任何差异时从程序中弹出的一种错误。使用这个 throw 关键字,我们可以通过解释程序运行时错误的任何原因来定义异常。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

让我们在下面看到更多细节:

语法

Java 中的 throw 语法如下:

throw exception_type ;

throw instance;

Throw 关键字在 Java 中如何工作?

在这里,我们可以通过示例检查关键字的使用方式并了解流程的工作原理。

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of A: ");
int a = sc.nextInt();
//Scanner b = new Scanner(System.in);
System.out.println("Enter value of B: ");
int b = sc.nextInt();
try {
c= a/b;
if(b==0)
{
throw new ArithmeticException();
}
System.out.println("Value of C is: " +c);
}
catch(ArithmeticException e) {
System.out.println("Caught this here");
}
finally {
}
System.out.println("Finally block is here");
}
}

让我们用一个经典且简单的除以零的例子来解决这个问题。

分析下面的代码:

  • 首先,我们导入了 Scanner 模块来获取用户输入的值。
  • 用户输入两个数字,即‘A’和‘B’。
  • 我们声明了一个变量“C”来存储数字“A”除以“B”后的商值。
  • 由于用户可以随机给出数字,我们可以想到用户可以将除数设为零的情况。
  • 接受这种情况,我们在 try 块中编写除法代码。
  • 如果条件是“B”值为零,那么我们将抛出异常。
  • 始终在“try”后面加上 catch 块。
  • 所以,这里我们提到了我们使用新命令通过关键字 throw 定义的异常。
  • 同样的异常也被catch了,我们只是打印了下面的一条语句,以便清楚地了解try、throw、catch、finally的流程。
  • 然后,最后,我们宣布我们的finally 块。众所周知,该块中的语句肯定会执行。

让我们检查下面的输出。

输出 1: 如果 b 值不为零。

Java 中的 throw 关键字

由于“B”值非零,所以没有执行 try、throw 和 catch 块。最后,无论是否创建异常,都会执行该块。

输出 2: 如果 b 值为零。

Java 中的 throw 关键字

突出显示的部分确保异常被成功抛出并被 catch 块捕获。

那么,让我们看下面的例子。

示例:

让我们看看如何在单个程序中抛出多个异常。

public class Main
{
public static void main(String[] args) {
String sun[] = {"chocolate", "honey", "sugar", "sweet", "bitter"};
String h = null;
try {
for (int i=0; i<=7; i++)
{
if(i > sun.length)
{
throw new ArrayIndexOutOfBoundsException();
}
System.out.println(sun[i]);
}
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println("Inside array index out of bounds exception catch block");
}
try{
sun[0]=h;
if (sun[0] == null)
{
throw new NullPointerException();
}
System.out.println("Value of sun[0] is: " +sun[0]);
}
catch(NullPointerException f)
{
System.out.println("Caught Null point exception");
}
finally {
System.out.println("executing finally");
}
}
}

上面,我们使用了两种不同类型的异常和 throw 关键字来解释程序。数组索引越界和空指针异常是我们在单个程序中使用的。

输出:

Java 中的 throw 关键字

投掷的重要性

这个 throw 关键字将帮助我们充当 try 和 catch 块之间的桥梁。它将有助于将程序的控制权从 try 块转移到 catch 块。

让我向您展示一个程序如何使用和不使用 throw 关键字的示例。

public class Main
{
public static void main(String[] args) {
try {
int a = Integer.parseInt ("Happy") ;
System.out.println("Will this get printed?");
} catch(NumberFormatException e) {
System.out.println("Number format exception of catch block");
}
System.out.println("Priting after catch block");
}
}

我们编写的代码没有 throw 关键字。但是我们使用代码来尝试捕获该块,该块将处理异常。那么,你知道上面的输出吗?

Java 中的 throw 关键字

意料之中吧?因此,它执行了代码,发现了异常,并且捕获了异常。

现在,当我们插入 throw 语句时,代码如何工作?下面就来看看吧。

public class Main
{
public static void main(String[] args) {
try {
int a = Integer.parseInt ("Happy") ;
throw new NumberFormatException();
System.out.println("Will this get printed?");
} catch(NumberFormatException e) {
System.out.println("Number format exception of catch block");
}
System.out.println("Priting after catch block");
}
}

只有突出显示的部分是上面两段代码之间的变化。

输出:

Java 中的 throw 关键字

是的,我们有一个编译错误,因为 throw 关键字后面的 print 语句无法访问。我们希望您通过这个例子准确理解“将控制权从 try 转移到 catch 块”的含义。

作为练习,尝试删除抛出关键字后的 print 语句并检查程序如何反应。

结论

因此,这就是异常处理时 throw 命令的作用。我确实注意到 THROW 和 THROWS 关键字之间有很大的区别。两者都与例外的概念一起使用。我们已经知道这个概念、如何以及在哪里可以使用“THROW”关键字。只需练习并尝试以不同的方式使用它即可。继续学习。

以上是Java 中的 throw 关键字的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn