First, it is clear that if there is no try, an exception will cause the program to crash.
And try to ensure the normal operation of the program, such as:
try{
int i = 1/0;
}catch (Exception e) {
........
}
A calculation, if the divisor is 0, it will be an error, if there is no try, the program directly crashes. With a try, you can let the program run, and the output why it went wrong!
Try, with the use of log4j will be very helpful for future maintenance of the program.
The code that needs to catch the exception is populated directly between the try and catch, and then an exception is caught if there is an exception that goes directly to the catch branch.
Such as:
try{
Code that needs to catch exceptions
} catch (Exception e) {
System.out.println (E.getmessage ());
}
Explanation: In the above code, if there is an error in "//code to catch the exception", the console output will be done directly.
Try and catch