1. Concept of anomalies
What is an exception? The program error is divided into two parts, and the compile time is coarse and the runtime error occurs. Compile-time error is the compiler when compiling the source code errors occur;
A run-time error is an error that occurs at run time when the compilation passes. This condition is called an exception.
For example: array out of bounds, divisor 0, file not found and so on.
The level of the exception:
Object
Throwable
Error
Exception
RuntimeException
IOException
2. How to resolve exceptions
A. Using Try...catch
Public class Usingtrycatch { publicstaticvoid main (String args[]) { int a = 5; int b = 0; int result = 0; Try { = A/ b; Catch (Exception e) { System.out.println (e); }}}
B. Using the throws or throw keyword
Public classUsingthrows { Public Static voidMain (String args[]) {Try{caculation caculation=Newcaculation (); } Catch(Exception e) {e.printstacktrace (); } } Public Static classcaculation{intA = 5; intb = 0; intresult = 0; PublicCaculation ()throwsexception{Result= A/b; SYSTEM.OUT.PRINTLN (result); } }}
Public classUsingthrow { Public Static voidMain (String args[]) {Try {//caculation caculation = new Caculation (); Throw NewException ("instanced Exception"); } Catch(Exception e) {e.printstacktrace (); }} Public Static classcaculation{intA = 5; intb = 0; intresult = 0; PublicCaculation ()throwsexception{Result= A/b; SYSTEM.OUT.PRINTLN (result); } }}
Use of Java exception classes