1. Manually Throwing Exceptions
Manually throwing an exception uses the throw, throws statements.
Throw statement:
is used to explicitly throw an exception, the execution process stops immediately after the throw statement, so the next statement is not executed.
When an exception is thrown, the system checks the nearest layer's try block to see if he has a catch clause that matches the type of the Throwabel instance.
If a match is found, control is transferred to the statement. If no match is found, the next level of Try statement is checked, and the loop continues until the outermost exception handler is executed.
Throws statement:
Used to handle multiple exceptions that a method might throw, throws must provide an exception list and a method definition.
Suppose the X () method calls another method Y (), and if Method Y () throws an exception and the exception is not processed, the following rules must be followed for the usage Method x ():
<1> declares the X () method with the Try/catch statement block to catch the thrown exception
<2> if the X () method does not have a try/catch block, the exception is passed to its own caller, the method that invokes the X () method. To avoid this situation it is best to include at least one Try/catch block in the X () method
2. Manually throwing custom exceptions
Built-in exceptions provided by the exception and error classes do not always capture all errors that occur in the program. Sometimes you need to create user custom exceptions, which are subclasses of the exception class.
The exception class does not define any of its own methods, but inherits all of its methods from the Throwable class. Any user-defined exception class that you create will have a method of that class.
Sample code: Java Code Package mypackage; import java.util.scanner; /** * Manual Throw Custom Exception sample * @author Glasses brother */ public class Ex Ceptiondemo { Public Exceptiondemo () {} public static void main (String [] args { new Exceptiondemo (). Demo (); } public void demo () { Scanner s = new Scanner ( system.in); System.out.print ("Please enter the length of the array:"); String str = s.next (); Exceptionclass EC = new Exceptionclass (str); s.close (); } } class exceptionclass{ protected Exceptionclass (String size) { try { this.checksize (size); catch (illegalnumberexception e) { System.out.println (e); new Exceptiondemo (). Demo (); } catch (Arraysizeexception e) { System.out.println (e); new Exceptiondemo (). Demo (); } } public void checksize (String size) throws Illegalnumberexception, Arraysizeexception { if (size.matches ("\\d")) { throw new Illegalnumberexception (); } int num = Integer.parseint (size); if (num<0) { throw new Arraysizeexception (); } int[] array = new int[num]; & nbsp; for (int i = 0; i < num; i++) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N bsp; Array[i] = i+1; System.out.println (Array[i]); } } } Class Illegalnumberexception extends numberformatexception{ private static final long Serialversionuid = 1l; public illegalnumberexception () { super ("Input is not a number.) "); } } class Arraysizeexception extends negativearraysizeexception{ private static final long Serialversionuid = 1l; public arraysizeexception () {  ; Super ("passed an illegal array length. "); } }
Package Mypackage;import java.util.scanner;/** * Manual Throw Custom Exception sample * @author glasses brother */public class Exceptiondemo {public Excepti Ondemo () {} public static void Main (string[] args) {new Exceptiondemo (). demo (); \ public void demo () {Scanner s = new Sc Anner (system.in); System.out.print ("Please enter the length of the array:"); String str = S.next (); Exceptionclass EC = new Exceptionclass (str); S.close (); }}class exceptionclass{protected Exceptionclass (String size) {try {this.checksize (size); catch (Illegalnumberexception e) {System.out.println (e); New Exceptiondemo (). Demo (); catch (Arraysizeexception e) {System.out.println (e); New Exceptiondemo (). Demo (); } public void Checksize (String size) throws Illegalnumberexception,arraysizeexception {if (size.matches ("\\d")) {THR ow new Illegalnumberexception (); int num = integer.parseint (size); if (num<0) {throw new arraysizeexception (); } int[] array = new Int[num]; for (int i = 0; i < num i++) {array[i] = i+1; System.out.printlN (Array[i]); }}}class Illegalnumberexception extends numberformatexception{private static final long serialversionuid = 1L; Llegalnumberexception () {super ("input is not a number.) "); }} Class Arraysizeexception extends negativearraysizeexception{private static final long serialversionuid = 1L; public Arra Ysizeexception () {super () passed an illegal array length. "); }}
Demo Results chart: