Java Exception handling Error
The study found that the best timing in the compilation phase was wrong, before the order. However, it is not possible to identify all errors during compilation, and the remaining issues must be resolved during the execution phase. This requires the wrong source to pass the appropriate information to a recipient in some way that the recipient knows how to handle the problem.
The purpose of exception handling in Java is to simplify the generation of large, reliable programs by using less than the current amount of code. And in this way you can be more confident: There are no unhandled errors in your app.
The word "exception" has my meaning to be surprised by it.
The problem arises, you may not know what to do, but you do know that you should not ignore it. You have to stop and see if someone else is somewhere else. Can deal with this problem. Simply don't have enough information to solve the problem in the current environment, so put the issue into a higher level environment. Here will make the right decision.
one of the obvious advantages of using exceptions is that it tends to reduce the complexity of error-handling code. Assuming that you do not use exceptions, you must check for specific errors. And in many parts of the program to deal with it.
If you use an exception, you do not have to check at the method call, because the exception mechanism will ensure that the error is caught. And, you just need to deal with errors in one place. In the so-called exception handler.
This approach not only saves code, but also separates the code describing what to do during normal operation and what to do with the problem.
Process of exception handling:
When an exception is thrown, there are a few things that can happen with it. First, the same as the creation of other objects in Java. The exception object will be created on the heap using new. The current execution path is then terminated, and a reference to the exception object is ejected from the current environment.
At this time The exception handling mechanism takes over the program and starts looking for an appropriate place to continue executing the program. The proper place is the exception handler, whose task is to recover the program from an error state so that the program can either execute in a different way, or continue execution.
Import java.util.*;p ublic class whocalled{static void F () {Try{throw new Exception ();} catch (Exception e) {for (stacktraceelement s:e.getstacktrace ()) System.out.println (S.getmethodname ());}} static void G () {f ();} static void H () {g ();} public static void Main (string[] args) {f (); System.out.println ("--------------------------------------"); G (); System.out.println ("--------------------------------------"); H ();}}
Finally clause:
The finally clause is always run, regardless of whether the exception is thrown.
Import Java.util.*;class Threeexception extends Exception{}public class finallyworks{static int count=0;public static void Main (string[] args) {while (true) {try{if (count++==0) throw new Threeexception (); System.out.println ("No exception");} catch (Threeexception e) {System.out.println ("threeexception");} Finally{system.out.println ("in-finally clause"); if (count==2) break;}}}}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Java Exception handling Error