Java throws 是一个用方法名称声明的关键字,异常名称方法在调用时可能会引发。声明关键字有助于异常处理,并让编译器知道特定方法抛出检查异常,必须在编译时声明该异常,例如 IOException 或 ClassNotFoundException。如果不使用 try-catch 块或 throws 关键字处理已检查的异常,编译器将引发编译时错误。该关键字还可以帮助程序员通过了解该方法抛出异常来开发高效的应用程序。
语法:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Access_specifierreturn_typemethod_namethrowsexception_name
这里,
例如:
public void my_method() throws MyException
错误和异常对于 Java 应用程序非常重要。它有助于确定是否发生了不合适的情况,例如语法错误或输入的输入与数据不匹配。此外,错误是不可避免的,只会导致编译错误并不可预测地停止应用程序执行;可以在一定程度上处理异常。
处理异常意味着如果异常发生,如何以正确且果断的方式停止执行。
有两种类型的例外:
因此,有两种方法来处理检查异常:
使用 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"); } } }
输出:
说明:在上面的示例中,我们声明了一个自定义异常,并使用 throw 关键字在 main 方法中显式抛出它。一旦控制进入方法控制并抛出异常,它就会直接进入catch块,执行这些语句,然后退出程序。
用方法名称声明此关键字告诉编译器该方法可以抛出异常。此关键字主要与 throw 关键字混淆,用于在我们的代码中或在处理自定义异常时故意抛出异常。
我们可以通过两种方式使用 throws 关键字:
首先,我们可以在抛出异常的方法的声明中使用 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"); } }
输出:
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.
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:
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.
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中文网其他相关文章!