Learning objectives:
Master abnormalityLearning content:
1. finally 2. throws 3. throw 4. User defined exceptionStudy time:
June 6, 2021Learning output:
1. 1 technical note 2. 2 CSDN technical blogsFinally
try { Possible exception codes }catch(Exception type e Used to receive thrown exception objects){ Exception occurred during capture processing }finally{ Whether or not there are exceptions,The code will execute } Follow up procedures continue
If there is no catch: try+finally, once an exception occurs, it will execute finally, and then the program crashes
throws
Throws: when defining a method, you can use the throws keyword to declare that the method does not handle exceptions, but is handed over to the method caller for processing.
Any method can declare exception types using the throws keyword, including abstract methods.
Subclasses override methods in the parent class. Subclass methods cannot declare and throw exceptions larger than the parent type (for compile time exceptions).
public abstract class ExceptionDemo4 { abstract void eat() throws Exception; } public class Demo extends ExceptionDemo4 { /* Method overrides the parent method The return value, method name and parameters must be the same The access modifier is greater than or equal to the permission of the parent method The declared exception must be less than or equal to the exception declared by the parent class */ @Override public void eat() throws UnsupportedEncodingException,Exception { } }
The method using throws must handle the declared exception when calling, either use try catch or continue to use the throws declaration.
public static void main(String[] args) { try { test1(); } catch (UnsupportedEncodingException e) { System.out.println("Encoding not supported"); }catch (ParseException p){ System.out.println("Date resolution exception"); }catch (NullPointerException n){ System.out.println("The object is empty"); } System.out.println("aaaaa"); } public static void test1() throws UnsupportedEncodingException, ParseException { test2(); } public static void test2() throws UnsupportedEncodingException, ParseException { test3(); } /* throws NullPointerException The runtime exception is declared as a runtime exception, which can be handled or not handled at the method call UnsupportedEncodingException Declared as a compile time exception, the call must be handled during compilation In general, throws is usually followed by compile time exception throwing Generally, the methods at the bottom choose to sound up */ public static void test3() throws NullPointerException, UnsupportedEncodingException, ParseException { String s = "China"; s.getBytes("gbk"); SimpleDateFormat sdf = new SimpleDateFormat(""); sdf.parse(""); }
throw
Throw keyword is used to explicitly throw an exception. When thrown, it is an instantiated object of an exception class.
In exception handling, the try statement captures an exception object, which can also be thrown by itself.
public static void main(String[] args) { //System.out.println(10/0);// An exception occurs when the virtual machine runs, and an object of this kind is thrown /* Exception in thread "main" java.lang.ArithmeticException: / by zero at com.ff.javaexception.day2.ExceptionDemo5.main(ExceptionDemo5.java:7) */ try { subFileType(null); } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); } } public static String subFileType(String fileName){ if(fileName==null){ //In the method body, the active display throws a pair of exception objects to inform (notify) the method call that there is a problem with the incoming data throw new NullPointerException("The file name is null Yes"); } return fileName.substring(fileName.lastIndexOf(".")+1); }
throws and throw
Throw is used in the method body to throw an actual exception object. After using throw, either try catch to catch the exception or throw to declare the exception.
throws is used in the method declaration to declare the possible exception types of the method. It can be
Multiple exception types are used to force these exceptions to be handled when the method is called.
Abstract methods can also use throws.
Custom exception
Function: mark business logic exceptions with user-defined exceptions to avoid confusion with standard exceptions.
Basic syntax:
public class exception class name extends Exception/RuntimeException{
public exception class name (String msg){
super(msg);
}
}
Custom exception classes often do not write other methods, but only overload the construction methods that need to be used
Inherit Exception. After throw is used in the method, try catch or
throws.
/* Custom exception class Score exception class */ public class ScoreException extends Exception{ public ScoreException() { super(); } public ScoreException(String msg) { super(msg); } }
public class Test { public static void main(String[] args) { ArrayList arrayList = new ArrayList(3); arrayList.add(11); arrayList.add(11); arrayList.add(11); arrayList.add(11); try { score(-10); } catch (ScoreException e) { e.printStackTrace(); System.out.println(e.getMessage()); } } public static String score(double score) throws ScoreException { if(score<0){ throw new ScoreException("The score is less than 0"); } if(score>100){ throw new ScoreException("The score is greater than 100"); } if(score>=90){ return "A"; } return "D"; } }