Details and application examples of java exceptions

  • 2020-05-30 20:17:51
  • OfStack

Abnormal java

Usage instances of exceptions (exception classification: Error(which is caused by JVM calling the system and can only modify the code) and Exception(which is caused by JVM and can be handled specifically)

1. If possible exceptions within a method, then can be abnormal through throw new corresponding exception class, and on the method statement throws exception class that may have been thrown to the caller, the caller can undertake exception handling, or continue to throw an exception by upper caller continue processing, if there is no exception to the whole process of any processing, so will be the default processing by JVM virtual machine

2. The caller can handle the exception with try()catch(){}, or continue with the exception with throws after the method, or with throw without handling the exception in the catch code block

3. Runtime exceptions RuntimeException can be declared without explicit exceptions

4. If the parent class method throws exceptions, if after the subclasses to rewrite method is also an exception is thrown, the exception must not be greater than that of the parent class exception class, if the parent class method without throwing an exception, and subclasses override method throws the exception, so at this time only try catch to capture this exception, but can also take in this exception catch block throw new RuntimeExcetion throw (), this method need not throws statement

5. Most of the time, exceptions do not need to be handled by the caller, who must have the ability to handle them

6. The exception should be wrapped into the type of exception that can be recognized by the caller on the upper level. It should be oriented to different callers and report different exception information

This is important in development

7. In the finally code block, it is common to release and close the resource. The open resource should be closed in the opposite direction, because there may be dependencies on the resource

8. If you do not declare an exception, the purpose is to prevent the caller from processing and to stop the caller's program, so you must modify the error code


public class ExceptionDemo {

 public static void main(String[] args) {
 //OutOfMemoryError Memory overflow error, 
 int[] i = new int[1024*1024*1024];
 System.out.println(i[1]);
 
 //ArrayIndexOutOfBoundsException Index out of bounds exception 
 int[] s = new int[2];
 System.out.println(s[2]);
 
 
 Calc calc = new Calc(); 
 // Let's say we catch an exception here 
 try {
  calc.run(4, 0);
  calc.run(4, -1);
 } catch (NegativeException e) {// You must first throw a custom subclass of the exception 
  e.printStackTrace();
  System.out.println(e.getMessage());
  //throw e;// You can continue to throw this exception 
 } catch (ArithmeticException e){// Throw the parent of a custom exception class 
  e.printStackTrace();
  System.out.println(e.getMessage());
  //throw e;
 } finally {
  System.out.println("finally It's going to happen ");
 }
 // If an exception is caught above, then the code can continue to execute, otherwise the code cannot continue to execute 
 System.out.println(" Can be executed to! ");
 
 try {
  calc.run(4, -1);
  
 } catch (NegativeException e) {
  e.printStackTrace();
  System.out.println(e.getMessage());
  return;
 } finally {
  System.out.println(" It will be implemented ");
 }
 System.out.println(" Out of execution ");// This line of code could not be executed 
 
 }

}

/**
 *  Custom exception 
 */
class NegativeException extends ArithmeticException{
 public NegativeException() {
 }
 public NegativeException(String msg) {
 super(msg);
 }
}

interface AA{
 public abstract void method();
}

class Calc implements AA{
 //ArithmeticException It's actually a runtime exception (RuntimeException) Even if it doesn't throws Declaration can also be compiled 
 public int run(int m,int n)throws ArithmeticException,NegativeException{
 if(n==0){
  throw new ArithmeticException(" The divisor cannot be zero 0");
 }else if(n<0){
  throw new NegativeException(" You can't have a negative divisor ");
 }
 int s = m/n;
 return s ;
 }

 @Override
 public void method() {
 try {
  int p = 4/0;
 } catch (ArithmeticException e) {
  e.printStackTrace();
  throw new RuntimeException();// Continue throwing the exception as a runtime exception 
 }
 }
}


Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: