Will the following code be executed after throw is executed?

1. After we try to catch the throw object

public void re(int i) {
        if (i > 5){
            this.i = i;
        }else {
            try {
                throw new Exception("Illegal data!");
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("123");
        }

The result shows that 123 is output

java.lang.Exception: Illegal data!
	at com.zsxfa.java.Ex3.re(ExcTest1.java:21)
	at com.zsxfa.java.ExcTest1.main(ExcTest1.java:10)
123

2. Do not try catch the throw object

  • 2.1
public void re(int i) throws Exception {
        if (i > 5) {
            this.i = i;
        }else {
            throw new Exception("Illegal data!");
            System.out.println("123");  
        }

The result shows that the compilation fails

  • 2.2
public void re(int i) throws Exception {
        if (i > 5) {
            this.i = i;
        }else {
            throw new Exception("Illegal data!");
        }
        System.out.println("123");

The result shows that the exception is thrown and 123 will not be output

java.lang.Exception: Illegal data!
	at com.zsxfa.java.Ex2.re(ExcTest.java:30)
	at com.zsxfa.java.ExcTest.main(ExcTest.java:11)
Illegal data!

expand

		try{
			return;
		}catch(){
			
		}finally{
		
		}
		return;

The above code is executed in this order:

The code segment first enters the try statement. Because there is no exception thrown, the code saves the content to return in memory, and then executes finally. Because there is no executable content in finally, it returns to try to execute return. The execution of the whole code segment ends, and the return after finally will not be executed.

notes

1,Whether there are exceptions or not, finally The code in the block will be executed;

2,When try and catch Zhongyou return When, finally Will still be implemented;

3,finally Yes return The following expression is executed after the operation (the value after the operation is not returned at this time,
Instead, save the value to be returned first, regardless of finally How about the code in? The returned value will not be changed
 Change, which is still the previously saved value), so the return value of the function is finally Determined before implementation;

4,finally It's best not to include return´╝îOtherwise, the program will exit in advance and the return value is not try or catch The return value saved in.

In the following code, when a runtime exception is thrown, will the following code be executed? Do you need to add a return statement after the exception?

public void add(int index, E element){
    if(size >= elements.length) {
        throw new RuntimeException("The sequence table is full and cannot be added"); 
        //return;  // Do you need it?
    }
    ....
}

To answer this question, I wrote several pieces of code and tested it. The results are as follows:

//Code 1
public static void test() throws Exception  {

    throw new Exception("Parameter out of bounds"); 
    System.out.println("After abnormality"); //Compilation error, "inaccessible statement"
}

//Code 2
try{
    throw new Exception("Parameter out of bounds"); 
}catch(Exception e) {
    e.printStackTrace();
}
System.out.println("After abnormality");//Can execute

//Code 3
if(true) {
    throw new Exception("Parameter out of bounds"); 
}
System.out.println("After abnormality"); //An exception is thrown and will not be executed

Summary:

  1. If an exception is thrown before a piece of code and the exception is not caught, the code will produce a compile time error "inaccessible statement". Such as code 1
  2. If an exception is thrown before a piece of code and the exception is caught by try... Catch, if no new exception is thrown in the catch statement at this time, the code can be executed. Otherwise, the same as Article 1. Such as code 2
  3. If an exception is thrown in a conditional statement, the program can be compiled, but the following statement will not be executed. Such as code 3

In addition, summarize the differences between runtime exceptions and non runtime exceptions:

  • Runtime exceptions are exceptions of RuntimeException class and its subclasses. They are non checked exceptions, such as NullPointerException, IndexOutOfBoundsException, etc. Because such exceptions are either system exceptions and cannot be handled, such as network problems;
    Either the program logic is wrong, such as null pointer exception; The JVM must stop running to correct this error, so the runtime exception can be handled by the JVM itself without being handled (caught or thrown up, of course). The Java Runtime will automatically catch the RuntimeException of the program throw, then stop the thread and print the exception.

  • Non runtime exceptions are exceptions other than RuntimeException. They all belong to Exception class and its subclasses. They are inspected exceptions. Non runtime exceptions must be handled (caught or thrown up). If they are not handled, the program will have a compilation error. In general, the exceptions with throws written in the API are not runtimeexceptions.

Common runtime exceptions:

Common non runtime exceptions:

Tags: Java Exception throw

Posted by imarockstar on Sat, 21 May 2022 23:16:39 +0300