Operations that will continue after java exception is catch

  • 2021-08-28 20:01:47
  • OfStack

I won't talk too much, let's just look at the code ~


import java.util.UUID; 
public class Test { 
 public static void main(String[] args) {
 try {
 int i = 2/0;
 } catch (Exception e) {
 e.printStackTrace();
 }
 
 try {
 System.out.println("try2");
 } catch (Exception e) {
 // TODO: handle exception
 }
 
// int i = 2/0;
// System.out.println("helo");
 } 
}

Print results:


java.lang.ArithmeticException: / by zero
at Test.main(Test.java:8)

try2


import java.util.UUID; 
public class Test { 
 public static void main(String[] args) {
// try {
// int i = 2/0;
// } catch (Exception e) {
// e.printStackTrace();
// }
// 
// try {
// System.out.println("try2");
// } catch (Exception e) {
// // TODO: handle exception
// }
 
 int i = 2/0;
 System.out.println("helo");
 } 
}

If an exception is not caught, execution will be terminated:

Print results:


Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:19)

Supplement: catch and throw are used simultaneously in Java

1. Application background

In practical application, Handling exceptions often requires more complicated handling-when an exception occurs, The exception cannot be completely handled by a single method, Several methods must cooperate to handle the exception completely, that is, in the current method where the exception occurs, the program can only partially handle the exception, and some processing needs to be done in the caller of the method, so the exception should be thrown again so that the caller of the method can catch the exception.

In order to realize this situation, the same 1 exception can be handled by multiple methods in cooperation, which can be done in combination with throw statement in catch block.

2. Examples of application

1 code example

AuctionTest.java


public class AuctionTest
{
 private double initPrice = 30.0;
 //  Because the method explicitly throws the AuctionException Anomaly, 
 //  So here you need to declare that you throw AuctionException Anomaly 
 public void bid(String bidPrice)
 throws AuctionException
 {
 double d = 0.0;
 try
 {
 d = Double.parseDouble(bidPrice);
 }
 catch (Exception e)
 {
 //  Here, the repair processing that can be performed on exceptions in this method is completed. 
 //  This is just printing exception trace stack information on the console. 
 e.printStackTrace();
 //  Throw the custom exception again 
 throw new AuctionException(" The bidding price must be numerical, "
 + " Can't contain other characters! ");
 }
 if (initPrice > d)
 {
 throw new AuctionException(" The auction price is lower than the starting price, "
 + " No bidding allowed! ");
 }
 initPrice = d;
 }
 public static void main(String[] args)
 {
 AuctionTest at = new AuctionTest();
 try
 {
 at.bid("df");
 }
 catch (AuctionException ae)
 {
 //  Capture again bid Gets or sets the exception in the. And handle the exception 
 System.err.println(ae.getMessage());
 }
 }
}

AuctionException.java


public class AuctionException extends Exception
{
 //  Parameterless constructor 
 public AuctionException(){}  // ① 
 //  Belt 1 Constructor for string parameters 
 public AuctionException(String msg) // ② 
 {
 super(msg);
 }
}

2 Running results


java.lang.NumberFormatException: For input string: "df"
 at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
 at java.lang.Double.parseDouble(Double.java:510)
 at AuctionTest.bid(AuctionTest.java:16)
 at AuctionTest.main(AuctionTest.java:39)

The bid price must be numeric and cannot contain other characters!

3 Results show that

After the above program bid corresponding to catch block captures the exception, the system prints the trace stack information of the exception, and then throws an AuctionException exception to inform the method caller to handle the AuctionException exception again.

The main method in all programs, that is, the bid method caller, catches the AuctionException exception again and outputs the exception detail information to the standard error output.


Related articles: