Examples and overviews of common exceptions [java]

Extension of exception examples

Common runtime exceptions:

  1. NullPointerException

    eg:

int [] arr=null;
System.out.println(arr[3]);
  • When a null pointer exception occurs, we go to look for an object to see if some null object has called some object's properties and methods.
  1. Array IndexOutOfBoundException

    eg:

int [] arr=new int[10];
System.out.println(arr[10]);
  • There are two types of IndexOutOfBoundException: array crossover and string crossover.
  1. Type Conversion Exception

‚Äč eg:

Object obj=new Date();
String str=(String)obj;//Reference variables of type Data cannot be converted to type String here, so type conversion exceptions will occur here
  1. NumberFormatException

    eg:

String str="123";
str="abc";
int num =Integer.ParseInt(str);//There is a format conversion exception here because the str string has an English letter
  1. InputMismatchException(eg:Scanner statement input mismatch)

  2. Arithmetic Exception

    eg:

int x=10;
int y=0;
System.out.println(x/y);//There's an arithmetic exception here because the tomb denominator is 0
  1. IllegalArgumentException
    eg:
SimpleDateFormat s =  new SImpleDateFormat(); //A SimpleDateFormat object in default format was created
String g = "123";
String g1 = s.format(g);  //IllegalArgumentException exception will occur here at this time
System.out.println(g1);
  • Our parameter invalid exception is thrown when our parameter can be passed into this method, but the parameter passed in does not have a solution for our method
    • For example, here our SimpleDateFormat objects call their format() method, at which point we can pass in Date-type objects in our format() method, as well as some overloaded methods. One of the method parameters is Object type, at which point we can pass all subclass objects of the Object class into our format() method as parameters. But at this point our parameters can be transferred in, but not necessarily our method provides code to manipulate this parameter, if not, this IllegalArgumentException exception will be thrown
    • Above we passed our string into our format() method, and our format() method is the formatting method, which converts our Date type data into our String type data. At this time we passed in a String type data, although our method allows it, an illegal parameter was passed in
  • IllegalArgumentException is a runtime exception
  1. IllegalMonitorStateException
    eg:
class Clerk{
    private int productCount = 0;
    public synchronized void product(){
        notify();
        if(productCount<20){
            productCount++;
            System.out.println(Thread.currentThread().getName()+"Production in progress"+productCount+"Product");

        }else {
            try {
                Clerk.class.wait(); //An IllegalMonitorStateException exception is thrown here
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}
  • The synchronization monitor in the synchronization method here is this. That is, an object of this class, which is a Clerk object that we instantiate. At this time, we call the wait() method through this class as a class object. At this time, our wait() method is not called through the synchronization monitor. This will throw the IllegalMonitorStateException exception
  • That is, when we use the wait() method, we have to hold the lock first, that is, use the synchronization monitor call before we can
    • That is, you have to get the money before you can go shopping. If we don't even get the money, then we can't buy anything.

These are all runtime exceptions

Examples of common compile-time exceptions are:

eg:

public Static void main(String [] args){
    File file = new File("hello.txt");
    FileInputStream  fis= new FileInputStream(file);//FileNotFoundException
    int data = fis.read();  //IOException
    while(data!=-1){
        System.out.println((char)data);
        data=fis.read();   //IOException
    }
    fis.close(); //Close Input Stream//IOException
}
  • The FileNotFoundException above is a subclass of IOException
  • This program will fail when javac.exe is compiled
  • An error during the compilation phase prevents the generation of the.class byte code file.

ParseException
eg:

SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);
String s  = "20-06-12";
Date d = sdf.parse(S); //A ParseException will be thrown here

  • Parsing exceptions is the fear that if we enter a string for parsing and do not follow the format of our SimpleDateFormat object, there will be a parsing exception
  • ParseException is a compile-time exception

Thread InterruptedException
eg:(Suppose we created a thread's object th)

th.sleep(); //This is to put our thread to sleep, at which point we will report a thread interrupt exception
th.wait();  //This is where our thread gets blocked and synchronization locks are released, at which point a thread interrupt exception is reported
th.join();  //This is where our thread gets blocked, our other thread finishes executing, and a thread interrupt exception is reported

  • Our thread interrupt exception is caused by our thread waiting for a long time for some reason, hibernating, or other suspended state, when another thread throws an InterruptedException exception when it terminates by calling the interrupt() method of the Thread class
  • This exception is a compile-time exception
  • We report an InterruptedException exception when we call the above methods, which requires us to write specific code to solve the problem, but it is not certain if this exception really happens here after we run it.

Tags: Java Back-end

Posted on Mon, 29 Nov 2021 15:54:03 -0500 by dthomas31uk