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

What is the form of JAVA throwing an exception?

2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the forms of JAVA throwing exception". In daily operation, I believe many people have doubts about the form of JAVA throwing exception. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "what is the form of JAVA throwing exception?" Next, please follow the editor to study!

The system automatically throws an exception

When there are some logic errors, doctrine errors or type conversion errors in the program statement, the system will automatically throw an exception.

Example one

Public static void main (String [] args) {int a = 5 per int b = 0 per system. Out.println (a / b);}

As a result, the system will automatically throw an ArithmeticException exception.

Exception in thread "main" java.lang.ArithmeticException: / by zeroat io.renren.modules.sys.controller.SysUserController.main (SysUserController.java:154)

Example two

Public static void main (String [] args) {String str = "abc"; System.out.println (Integer.parseInt (str));}

As a result, the system will throw a NumberFormatException exception.

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc" at java.lang.NumberFormatException.forInputString (NumberFormatException.java:65) at java.lang.Integer.parseInt (Integer.java:580) at java.lang.Integer.parseInt (Integer.java:615) at io.renren.modules.sys.controller.SysUserController.main (SysUserController.java:153)

II. Throw

Throw is a statement that throws an exception, usually inside the code. When a certain logic error occurs in the program, the same program actively throws a specific type of exception.

Public static void main (String [] args) {String str = "NBA"; if (str.equals ("NBA")) {throw new NumberFormatException ();} else {System.out.println (str);}}

As a result, the system will throw a NumberFormatException exception.

Exception in thread "main" java.lang.NumberFormatExceptionat io.renren.modules.sys.controller.SysUserController.main (SysUserController.java:154)

III. Throws

Throws is that a method may throw an exception (used when declaring a method, indicating that the method may throw an exception)

Public void function () throws Exception {.}

When a method may throw an exception, it is used for throws to declare the exception that may be thrown, and then give it to the upper layer to handle the method program that calls it.

Public static void testThrows () throws NumberFormatException {String str = "NBA"; System.out.println (Integer.parseInt (str));} public static void main (String [] args) {try {testThrows ();} catch (NumberFormatException e) {e.printStackTrace (); System.out.println ("non-numerically straight types cannot cast");}}

Running result

Java.lang.NumberFormatException: For input string: "NBA" at java.lang.NumberFormatException.forInputString (NumberFormatException.java:65) at java.lang.Integer.parseInt (Integer.java:580) at java.lang.Integer.parseInt (Integer.java:615) at io.renren.modules.sys.controller.SysUserController.testThrows (SysUserController.java:153) at io.renren.modules.sys.controller.SysUserController.main (SysUserController.java:158) non-numerically straight types cannot be cast

Comparison between throw and throws

1. Throws appears in the head of the method function, while throw appears in the function body.

2. Throws indicates the possibility of an exception, which does not necessarily occur. Throw throws an exception, while executing throw must throw an exception object.

3. Both are negative ways of handling exceptions (the negativity here does not mean that this is a bad way). Exceptions are just thrown or may be thrown, but the exception is not handled by the function. The real exception handling is handled by the upper layer of the function.

Programming habit

1. When writing a program, you usually use try {... } catch {... } to capture it and deal with it

2. Use try {… } catch {... } after catching the exception, be sure to check the catch {... }, even if it is the simplest output statement, or stack input e.printStackTrace ()

3. If you are catching an exception in the IO input and output stream, be sure to use the try {... } catch {... } followed by finally {… } close the input and output stream

4. If an exception is thrown with throw in the function, it is best to add the throws exception declaration in the function name and give it to the upper function that calls it to handle it.

At this point, the study of "what are the forms in which JAVA throws exceptions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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