In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use Java exception handling try catch for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
When we make an error in compiling and running the program, the compiler throws an exception. Throwing exceptions is much more flexible than terminating programs because Java provides a handler (handler) that "catches" exceptions to handle exception cases.
If no processor mechanism is provided, the program terminates and prints a message on the console giving the type of exception .L
For example: the use of NULL references or array out of bounds and so on.
There are two types of exceptions: unchecked exceptions and checked exceptions. For checked exceptions, the handler checks to see if a processor is provided.
However, there are many exceptions, such as accessing null references, that are unchecked. The compiler does not check to see if a processor is provided for these errors. After all, you should take a rigorous approach to writing code, avoiding these errors in turn, rather than spending energy on writing exception handlers.
Cut the crap, show coder:
/ * try catch: handle exceptions by yourself
* try {
* Code that may have an exception
*} catch (exception class name An e) {
* if an exception of type An of the exception class occurs, execute the code
*}. (there can be more than one catch)
* finally {
* the code that must eventually be executed (for example, code that releases resources)
*}
* the order in which the code is executed:
* the code in 1.try interrupts execution starting from the line where the exception occurs
* 2. Execute the code within the corresponding catch block
* 3. Continue to execute the code after the try catch structure
* Note:
* 1. If the exception class in catch has a subparent relationship, then the subclass should come first and the parent class should be last.
* 2. If there is a return statement in the end, then the final returned result must be the return value in the final.
* 3. If there is a return in the final statement, the unhandled exception will be swallowed
* Note on rewriting:
* 1. A son can't be more capable than his father.
* 2. A son is more open than his father.
* 3. The son cannot cause more trouble than the father (the exception type of the subclass cannot be the parent type of the exception of the parent class)
* exception class Api:
* 1. GetMessage (): gets the exception description information string
* 2. ToString (): returns the package path and class of the exception class
Name and exception description information string
* 3. PrintStackTrace (): print stack information in addition to the printed toString information
* /
The above comment comes from this blogger on the Internet. I think it's well written. Portal address: https://blog.csdn.net/wanghuiwei888/article/details/78818203.
Package Bird; import java.io.FileNotFoundException;import java.io.FileReader; / / public class TestYc {public static void main (String [] args) {try {FileReader fr = new FileReader ("c:/abc.txt") } catch (FileNotFoundException e) {/ / printout exception e.printStackTrace () } Mother mother = new Mother (); mother.bbb (); / / 1. The file under "c:/abc.txt" / * try {FileReader fr = new FileReader ("c:/abc.txt");} catch (FileNotFoundException e) {/ / printout exception e.printStackTrace () } * / int [] arr = new int [] {1 System.out.println (arr [2]);} class Mother {private Boy b = null; / / constructor public Mother () {b = new Boy () } public void bbb () {/ / TODO Auto-generated method stub / / calls the method try {b.aaa ();} catch (FileNotFoundException e) {e.printStackTrace () with an exception } class Boy {/ / throws throws an exception to the upper caller public void aaa () throws FileNotFoundException {FileReader fr = new FileReader ("c:/abc.txt");}}
Run the screenshot, the file opening does not exist:
Screenshot of normal operation:
This is the end of the article on "how to use Java exception handling try catch". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.