www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Exception Handling in Java 2 Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
We will continue our discussion on Exception handling in this lesson. In this lesson we will learn to code Exception handlers.

Exception Handler
Exception handler in Java is similar to C++ except that the parameter of every catch must be present and its class must be descendent of Throwable. The syntax of try is exactly same as C++ except that there is an optional finally clause as well.

Consider the following example:
class MyException extends Exception {
   public MyException() { }
   public MyException(String message) {
      super (message);
This exception can be thrown with
throw new MyException();


MyException myExceptionObject = new MyException();
throw myExceptionObject;

Binding of exception is also similar to C++. If an exception is thrown in the compound statement of try construct, it is bound to the first handler (catch function) immediately following the try clause whose parameter is the same class as the thrown object or an ancestor of it. Exceptions can be handled and then re-thrown by including a throw statement without an operand at the end of the handler. To ensure that exceptions that can be thrown in a try clause are always handled in a method, a special handler can be written that matches all exceptions. For example:

catch (Exception anyException) {

Next >>> Lesson No. 19: Exception Handling in Java 3

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.