首页 > Java > java教程 > 正文

Java 中的 throw 关键字

WBOY
发布: 2024-08-30 15:22:07
原创
305 人浏览过

Java throws 是一个用方法名称声明的关键字,异常名称方法在调用时可能会引发。声明关键字有助于异常处理,并让编译器知道特定方法抛出检查异常,必须在编译时声明该异常,例如 IOException 或 ClassNotFoundException。如果不使用 try-catch 块或 throws 关键字处理已检查的异常,编译器将引发编译时错误。该关键字还可以帮助程序员通过了解该方法抛出异常来开发高效的应用程序。

语法:

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

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

Access_specifierreturn_typemethod_namethrowsexception_name
登录后复制

这里,

  • Access_specifier:这是指告诉 JVM 从哪里可以访问特定方法的关键字。例如,私有/公共或受保护。
  • Return_type:这是指告诉被调用方法返回的对象的数据类型的关键字,例如 int、Boolean、String 等
  • Method_name:指需要调用的方法名称。
  • exception_name 可以是进程在运行程序流时可能引发的内置或自定义异常。

例如:

public void my_method() throws MyException
登录后复制

Java 中的 Throws 关键字如何工作?

错误和异常对于 Java 应用程序非常重要。它有助于确定是否发生了不合适的情况,例如语法错误或输入的输入与数据不匹配。此外,错误是不可避免的,只会导致编译错误并不可预测地停止应用程序执行;可以在一定程度上处理异常。

Java 中的 throw 关键字

处理异常意味着如果异常发生,如何以正确且果断的方式停止执行。

有两种类型的例外:

  • 未检查异常:这些类型的异常是运行时异常,如果在代码中处理它们,则编译器不会检查它们。示例 - 算术异常、IndexOutOfBoundsException 等。对这些异常使用 throws 关键字是没有意义的。
  • 已检查异常:这些是编译器在编译时检查的异常类型,以查看它们是否正在被处理。因此,如果不处理这些异常,编译器将抛出错误 - 未处理的异常类型。示例 – IOException、ClassNotFoundException。

两种处理方式

因此,有两种方法来处理检查异常:

1.尝试捕捉

使用 try-catch,将可能引发异常的语句放入 try 块中,如果引发异常,控制权将转到 catch 块中的语句来执行它们。这样,我们就可以在发生异常时控制应用程序的流程。

代码:

//package Proc;
class MyCustomeException extends Throwable{
MyCustomeException(String s){
super(s);
}
}
public class prac1
{
public static void main(String[] args)    {
try{
System.out.println("Now exception is being raised");
throw new MyCustomeException("Custom exception is thrown");
}
catch(MyCustomeException e){
System.out.println("Here exception is caught and handled");
}
}
}
登录后复制

输出:

Java 中的 throw 关键字

说明:在上面的示例中,我们声明了一个自定义异常,并使用 throw 关键字在 main 方法中显式抛出它。一旦控制进入方法控制并抛出异常,它就会直接进入catch块,执行这些语句,然后退出程序。

2.抛出关键字

用方法名称声明此关键字告诉编译器该方法可以抛出异常。此关键字主要与 throw 关键字混淆,用于在我们的代码中或在处理自定义异常时故意抛出异常。

  • 仅抛出关键字让我们可以在发生异常时执行语句。它不能用来避免异常的发生。因此,它用于异常处理。
  • throws 关键字经常与 throw 混淆,用于显式抛出异常。而 throws 就是用来处理的。
  • 如果出现异常,row关键字可以帮助程序员让程序顺利高效的运行。

在 Java 中实现 Throws 关键字的示例

我们可以通过两种方式使用 throws 关键字:

示例#1

首先,我们可以在抛出异常的方法的声明中使用 throws。

代码:

//package Proc;
public class prac1
{
public static void main(String[] args)throws IllegalAccessException
{
System.out.println("Hello Everyone lets start our learning with throws keyword");
throw new IllegalAccessException("My Exception");
}
}
登录后复制

输出:

Java 中的 throw 关键字

Explanation: In the above example, we have used the throws keyword to handle the exception being thrown using the throw keyword. In case an exception is raised, it will not prevent the program’s abnormal termination; instead, it helps to convince the compiler. Also, it helps to inform the programmer that the method might throw an exception and needs exception handling.

Example #2

Second, we use a try-catch block while calling a method that is likely to throw an exception. We have made a custom exception here named MyCustomeException that is being thrown by the method throwsDemo.

Code:

//package Proc;
class MyCustomeException extends Throwable{
MyCustomeException(String s){
super(s);
}
}
public class prac1
{
static void throwsDemo() throws MyCustomeException
{
System.out.println("We are Inside the method");
throw new MyCustomeException("Custom exception is thrown");
}
public static void main(String[] args)throws IllegalAccessException
{
try{
System.out.println("Now exception is being raised");
throwsDemo();
}
catch(MyCustomeException e){
System.out.println("Here exception is caught and handled");
}
}
}
登录后复制

Output:

Java 中的 throw 关键字

Explanation: In the above example, one method throws a new exception. This is indicated using the throws keyword. But when the method is called in the main method, we can use a try-catch block or declaring a throws keyword with the main method to handle the exception.

Conclusion

Throws keyword is used for exception handling in Java, where one needs to handle the flow of the program when a checked exception occurs. This is different from the throw keyword and must only be used with the checked exception since it does not prevent the occurrence of an exception but helps the way that must execute if one occurs.

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

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!