Five keywords for Java Exception Handling and java Exception Handling
Exception: some are caused by user errors, some are caused by program errors, and others are caused by physical errors.
Keyword for exception handling: try, catch, finally, throw, throws
Note:
- The error is not an exception, but an issue that is out of programmer control.
- All Exception classes are subclasses inherited from the java. lang. Exception class.
- The exception class has two main subclasses: The IOException class and the RuntimeException class.
- Java has many built-in exception classes.
Exception classification:
- The user entered invalid data.
- The file to be opened does not exist.
- The connection is interrupted during network communication, or the JVM memory overflows.
Syntax:
Try {// code block to be listened on}
Catch (exception type Exception name/e) {// handle the error code block caught by the try listening. throw Exception name/e; // thorw indicates throwing an exception throw new exception type ("Custom ");}
The statements in the finally {// finally block will be executed no matter whether the exception occurs or not.} The method name returned by the modifier () throws exception type {// throws is only used to declare the exception, whether to throw is determined by the method caller // code block}
Code example: (try and catch and finally)
Public class ExceptionTest {public static void main (String [] args) {custom input = new partition (System. in); try {// listening code block int a = input. nextInt (); int B = input. nextInt (); double sum = a/B; System. out. println (sum);} catch (InputMismatchException e) {System. out. println ("only numbers allowed");} catch (ArithmeticException e) {System. out. println ("the denominator cannot be 0");} catch (Exception e) {// Exception is the parent class System of all exceptions. out. println ("Other exceptions occurred");} finally {// whether or not an exception occurs, finally will be executed. out. println ("program ended ");}}}
Code example: (throw keyword)
Import java. util. inputMismatchException; import java. util. extends; public class ExceptionTest {public static void main (String [] args) {extends input = new evaluate (System. in); try {// listening code block int a = input. nextInt (); int B = input. nextInt (); double sum = a/B; System. out. println (sum);} catch (InputMismatchException e) {// catch (exception type Exception name) System. out. println ("only numbers allowed"); throw e; // throw the catch exception // throw new InputMismatchException (); same as} catch (ArithmeticException e) {System. out. println ("the denominator cannot be 0"); throw new ArithmeticException ("the denominator is 0 and an Exception is thrown"); // ArithmeticException thrown} catch (Exception e) {// Exception is the parent class System of all exceptions. out. println ("Other exceptions occurred");} finally {// whether or not an exception occurs, finally will be executed. out. println ("program ended ");}}}
Code example: (throws)
Public class Throws {int a = 1; int B = 0; public void out () throws ArithmeticException {// you can declare multiple exceptions that may be thrown, comma Separated try {// listening code block int sum = a/B; System. out. println (sum);} catch (ArithmeticException e) {System. out. println ("the denominator cannot be 0");} finally {// whether or not an exception occurs, finally will be executed System. out. println ("program ended") ;}} public static void main (String [] args) {Throws t = new Throws (); t. out (); // call method throw new ArithmeticException ("the denominator is 0 and an exception is thrown "); // determines whether to throw an exception based on the called method/** method 2 * // ArithmeticException a = new ArithmeticException ("the denominator is 0 and an exception is thrown "); // throw ;}}