Exception handling (also known as error handling) features
Used to handle anomalies in software or information systems (i.e., certain special conditions that go beyond the normal execution of a program).
Try....catch .... Just a common method of exception handling
try{
Actions that could cause an exception
}catch{
Defining Exception handlers
}
Give me a chestnut.
1 Public classABC {2 3 Public Static voidMain (String arg[]) {4 intD;5 intA;6 Try{7D=0; 8A=1;9a=a/D; The denominator is 0 and the code must have an exceptionTenSYSTEM.OUT.PRINTLN ("Output Try statement"); One}Catch(Exception e) { ASYSTEM.OUT.PRINTLN ("Output Catch statement"); - } -System.out.println ("Over"); the } - -}
Execute code Result:
Output Catch statement
Over
It executes the contents of the Catch statement
Change the above d=0 to d=1
1 Public classabc{2 3 Public Static voidMain (String arg[]) {4 intD;5 intA;6 Try{7d=2;8A=1;9a=a/D;TenSYSTEM.OUT.PRINTLN ("Output Try statement"); One}Catch(Exception e) { ASYSTEM.OUT.PRINTLN ("Output Catch statement"); - } -System.out.println ("Over"); the } - -}
Execute code Result:
Output Try Statement
Over
It executes the contents of the Try Statement
Java try Catch Exception Handling Learning