In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of exception handling in Java, which is very detailed and has certain reference value. Friends who are interested must finish it!
Definition of exception
In java, an exception is an error that occurs when java is compiled, run, or run
There are three kinds in total: 1. Compilation error 2. Run error 3. Logic error
1. The compilation error is because the program does not follow the syntax rules, and the compiler can find and prompt us the cause and location of the error, which is also a problem often encountered by beginners when they first come into contact with the programming language.
two。 The runtime error is due to the fact that the runtime environment finds an operation that cannot be performed when the program is executing.
3. The logic error is due to the fact that the program does not execute in the expected logical order. Exception refers to errors that occur when the program is running, and exception handling is to handle and control these errors.
Package org.oracle.test;public class TryDemo9 {public static void main (String [] args) {System.out.println (1 System.out.println 0); / 0 cannot be divided by System.out.println ("Hello"); / / the program catches an exception, the program terminates, and does not execute}}
When an exception is encountered during code execution, it is terminated.
The running result will tell us the type and location of the exception.
Classification of anomalies
RuntimeException: runtime exception, which is usually not handled manually. If something goes wrong, it will be dealt with again.
Other Exception: must be handled manually.
Error: generally refers to system-level errors.
Exception handling method try. Catch processes package org.oracle.test;import java.util.Scanner;public class TryDemo2 {public static void main (String [] args) {System.out.println (testOne ()) } / * * when there are return statements in try, catch and finally code blocks, * the program will execute the renturn statement in finally regardless of whether it reports an error or not * / public static double testOne () {try {Scanner sc = new Scanner (System.in) System.out.println ("Please enter the first number:"); int a = sc.nextInt (); System.out.println ("Please enter the second number:"); int b = sc.nextInt (); int result = a / b / / System.out.println ("result:" + result); System.out.println ("try block executed"); return result;} catch (Exception e) {e.printStackTrace (); return 0 } finally {return-1000;}
Running result:
Try-- is used to monitor. The code to be listened for (the code that may throw an exception) is placed within the try statement block, and when an exception occurs within the try statement block, the exception is thrown.
Catch-- is used to catch exceptions. Catch is used to catch exceptions that occur in try statement blocks.
Blocks of finally-- finally statements are always executed. It is mainly used to recover material resources (such as database connections, network connections and disk files) opened in try blocks.
Throw and throws
Throws indicates that the method is ready to throw an exception
Throw means to throw an exception
Package org.oracle.test;import java.util.InputMismatchException;public class TryDemo8 {public static void main (String [] args) {try {testThree ();} catch (Exception e) {System.out.println (e.getMessage ()); e.printStackTrace () } public static void testOne () throws InputMismatchException {throw new InputMismatchException ();} public static void testTwo () throws Exception {try {testOne ();} catch (InputMismatchException e) {throw new Exception ("New exception 1", e) } public static void testThree () throws Exception {try {testTwo ();} catch (Exception e) {Exception E1 = new Exception ("new exception 2"); e1.initCause (e); throw E1 }}}
Running result:
Custom exception
Custom exception: directly inherits Exception or RuntimeException to implement custom exception
Package org.oracle.test;import java.util.Scanner;public class TryDemo6 {public static void main (String [] args) {try {hotelRule ();} catch (Exception e) {e.printStackTrace () }} / * * Kidi opened a couple hotel. The rules of the Kidi hotel: customers under 18 and over 80 need family members to accompany them to stay * / public static void hotelRule () throws Exception {Scanner sc = new Scanner (System.in); System.out.println ("Please enter customer age:") Int age = sc.nextInt (); if (age
< 18 || age >80) {throw new HotelException ();} else {System.out.println (successful check-in);}} package org.oracle.test / * * Custom exceptions should be inherited from Exception * / public class HotelException extends Exception {public HotelException () {super ("unable to check in due to age");}}
Running result:
The above is all the content of the article "sample Analysis of exception handling in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to 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.
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.