First look and then like, give yourself a little time to think, if it helps you, wechat search [program workplace] pay attention to this persistent workplace programmer. What I have: career planning guidance, skills enhancement methods, endless career stories, personal growth experience.

1, introduction,

Exceptions and errors both inherit from the Throwable class. In Java, only instances of Throwable can be thrown or caught, which is the basic type of Exception handling.

What’s the difference between the two:

An Exception is an Exception that can be expected when a Java program is running. We can get such an Exception and handle it out of business. (What we usually do is catch or throw this exception.)

An Error is an unexpected exception that occurs during the execution of a Java program. When such an exception occurs, the JVM cannot handle or recover from it. This makes it impossible to catch exceptions like OutOfMemoryError, NoClassDefFoundError, and so on. (This will cause our program to crash, directly leading to the crash of the project)

2, the subclass

So what are the subclasses of these two exceptions?

1, the Exception

(1) RuntimeException (2) NullPointerException (3) ClassCaseException (4) SecurityException (5) IOExceptionCopy the code

2, the Error

(1) OutofMemoryError (2) StackOverFlowError (3) VirtualMachineError (4) LinkageError (5) NoClasDeffoundError (6) ExceptionInInitial izerErrorCopy the code

We know what subclasses are involved in both of them, as we said at the beginning

Exception and Error both inherit from the Throwable class

3, Throwable

We must understand the Elements in the Java language that operate Throwable and know how to use them. Basic syntax, such as try-catch-finally blocks, throws, throws, and so on, is a must. Also know how to handle typical scenarios.

Throw exists in the code block of a method, while throws exists outside the method, usually after the method name throws XXXException.

In the first place, try-catch-finally is used. In the first place, try-catch-finally is used. In the second place, try-catch-finally is used

try{ retrun 3; }catch{ e.printStackTrace(); }finally{ return 4; } // In the above case, the actual return is 4; try{ int x = 3; retrun x; }catch{ e.printStackTrace(); }finally{ x++; } // In the above case, 3 is actually returned;Copy the code

If we find that finally is going to go, we usually need to remove or release exceptions in finally.

1. Catching an exception Catching an exception is when we just throw it away and leave it there. The following cases:

package com.company.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; public class Exception_Error { public static void main(String[] args) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { } }}Copy the code

2. Throw an exception

package com.company.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; public class Exception_Error { public static void main(String[] args) { try{ throwChecked(3); }catch(Exception e) { System.out.println(e.getMessage()); } throwRuntime(-3); } // This method throws an Exception object, Public static void throwChecked(int A) throws Exception {if(a < 0) {throw new Exception(" the value of a should be greater than 0 "); }} // This method throws a RuntimeException object, Public static void throwRuntime(int a) {if(a < 0) {throw new RuntimeException("a should be greater than 0 "); }}}Copy the code

A throw statement is used in a method body to indicate that an exception is thrown and is handled by a statement in the method body

A throw is the action of specifically throwing an exception, so it throws an exception instance.

Throw is used with either a try-catch-finally statement or throws

If a RuntimeException is thrown, you can display the use of a try… Catch can also be ignored

3. Two basic principles of exception handling

  • Try not to catch generic exceptions like Exception, but rather specific exceptions.

  • Don’t swallow anything unusual.

I am a persistent workplace programmer, wechat search [program workplace] follow me. Don’t forget the triple link, like, like, leave a comment, feel free to give, I don’t choose. Note: If there are any problems with the article, please kindly correct them.