when the program is running, we cannot guarantee that all the services and methods are correct, and if an error occurs, it will cause the program to crash, so you need to handle some predictable errors, throw an exception by throwing, and then use try. Catch.. The method to be executed is enclosed, which captures the exception that the method might throw and processes Throw: Throws an exception (only the method throws an exception to be try...catch ...) Capture)throw new Fileistoosmallexcepton (); Catching Exceptions:try{the method to be executed}catch (xxexception e) {action when a xxexception exception is received}catch (Exception e) {code that executes when a exception exception is received can catch multiple exceptions, but the scope of the large cannot be above, only below (the exception above cannot contain the exception type of the catch later) }final{the part that executes whether or not an exception occurs}Description:1.catch () is an exception type exception parameter E, which only executes when an exception is thrown2. All exception types are inherited from exception, and exception inherit from Throwable. So you can use exception to catch all types of exceptions, exceptions have methods GetMessage () and Printstacktrace ()3. If the method declares a throw exception, the compiler prompts you to use Try...catch when calling the method ... (except RuntimeException)4. If final is added after the catch, the code in final{} is definitely executed, regardless of whether there is an exception (if a try or catch has a return, the final is also executed first and then return)5. Catch exception can write multiple catch, catch different exception types, but the size is very important, the scope of the large drop surface, if the small drop face will never be executed do not want to handle exceptions:If you do not want to handle the exception, throw the exception that may occur after the method namepublic void Foo () throws reallybadexception{call a risky method, this method may throw a Reallybadexception exception} But if the main function also avoids the exception by throw, if the exception is true, the program goes wrong.
Java Basics VI [exception handling] (read head first Java record)