Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to resolve the difference between the keywords of the two mechanisms of handling exceptions in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to analyze the difference between the keywords of the two mechanisms of handling exceptions in Java. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

What is the difference between throws and throw in exception handling?

Throws: a method is declared on a method, not processed, but uploaded, and handled by whoever calls it.

Throw: is to specifically throw an exception type.

Chestnut of throws:

Throws, that is, this method may produce an exception, and I just declare it out, I do not handle it myself, if someone calls it, you can know that this method may throw an exception, if I call it, I have to handle it, or then throws.

The format is: method name (parameter) throws exception class 1, exception class 2.

Class Math {public int p (int iMint j) throws Exception {int tactile j; return t;} public class ThrowsDemo {public static void main (String args []) throws Exception {Math m=new Math (); System.out.println ("start operation:" + m.p (10jue 2));}}

Throw: in a method with exceptions, you can catch or throws

Note throws: once executed, the program immediately moves to the exception handling phase, the following statements are no longer executed, and the method no longer returns a meaningful value.

Public class TestThrow {public static void main (String [] args) {try {/ / calls the method with throws declaration and must explicitly catch the exception / / otherwise, you must declare again in the main method that throwChecked (- 3) is thrown;} catch (Exception e) {System.out.println (e.getMessage ());} / / call the method that throws the Runtime exception either explicitly catch the exception or ignore the exception throwRuntime (3) } public static void throwChecked (int a) throws Exception {if (a > 0) {/ / throw an Exception exception / / the code must be in a try block or throw new Exception in a method with a throws declaration ("the value of an is greater than 0, does not meet the requirements") }} public static void throwRuntime (int a) {if (a > 0) {/ / throws a RuntimeException exception by itself, either explicitly catches the exception / / or completely ignores the exception, and hands the exception to the method caller to handle throw new RuntimeException ("the value of an is greater than 0, does not meet the requirements");}

The above is how to analyze the difference between the keywords of the two mechanisms of handling exceptions in Java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report