Home>

A java exception is an object that describes an exception (that is, an error) that occurred in a code segment.When an abnormal situation occurs,An object representing the exception is created and thrown in the method that caused the error. The method can choose to handle the exception itself or pass it on.In both cases,The exception is caught and handled. The exception may be generated by the java runtime system,Or it is generated by your manual code.The exception thrown by java is related to a basic error that violates the language specification or exceeds the limits of the java execution environment.Hand-coded exceptions are basically used to report error conditions for method callers.

Java exception handling is controlled by 5 keywords:try, catch, throw, throws, and finally. Here's how they work.The program declares that the exception monitoring i want is contained in a try block. If an exception occurs in the try block,It was thrown.Your code can catch this exception (with catch) and handle it in some reasonable way.System generated exceptions are automatically thrown by the java runtime system.Throw an exception manually,Use the keyword throw. Any exception thrown by a method must be defined by a throws clause.Any code that is absolutely executed before the method returns is placed in a finally block.

Here is the general form of an exception handling block:

try {
  //block of code to monitor for errors
}
catch (exceptiontype1 exob) {
  //exception handler for exceptiontype1
}
catch (exceptiontype2 exob) {
  //exception handler for exceptiontype2
}
//...
finally {
  //block of code to be executed before try block ends
}

Here, exceptiontype is the type where the exception occurred.

All exception types are subclasses of the built-in throwable class. Therefore, throwable is at the top of the exception class hierarchy.Immediately below the throwable are two subclasses that break the exception into two different branches.One branch is exception.

This class is used for exceptions that the user program may catch.It is also a class that you can use to create your own user exception subclasses.There is an important subclass of runtimeexception in the exception branch. This type of exception is automatically defined for programs you write and includes errors such as division by zero and illegal array indexing.

Another type of branch has error as the top layer,error defines exceptions that are not expected to be caught by the program under normal circumstances.Error type exceptions are used by the java runtime system to display errors related to the runtime system itself.Stack overflow is an example of such an error.This chapter will not discuss exception handling about error types.Because they are usually catastrophic fatal errors,Not beyond your program's control.

  • Previous Comprehensive interpretation of inner classes in Java programming
  • Next The most complete collection of commonly used regular expressions-including check digits, characters, some special needs, etc