Exception Objects: stack trace : Exception « Development « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Regular Expressions
8. Collections
9. Thread
10. File
11. Generics
12. I18N
13. Swing
14. Swing Event
15. 2D Graphics
16. SWT
17. SWT 2D Graphics
18. Network
19. Database
20. JSP
21. JSTL
22. Servlet
23. Web Services SOA
24. Email
25. J2EE Application
26. XML
27. Design Pattern
28. Log
29. Apache Common
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Tutorial » Development » Exception 
6. 18. 8. Exception Objects: stack trace

public class MainClass {
  public static void main(String[] args) {
    int[] array = new int[]{1,0,2};
    int index = 0;
    try {
      System.out.println("\nFirst try block in divide() entered");
      array[index + 2= array[index]/array[index + 1];
      System.out.println("Code at end of first try block in divide()");
    catch(ArithmeticException e) {
      System.err.println("Arithmetic exception caught in divide()\n" +
                         "\nMessage in exception object:\n\t" +
                          e.getMessage());
      System.err.println("\nStack trace output:\n");
      e.printStackTrace();
      System.err.println("\nEnd of stack trace output\n");
    catch(ArrayIndexOutOfBoundsException e) {
      System.err.println("Index-out-of-bounds exception caught in divide()\n" +
                         "\nMessage in exception object:\n\t" + e.getMessage());
      System.err.println("\nStack trace output:\n");
      e.printStackTrace();
      System.out.println("\nEnd of stack trace output\n");
    finally {
      System.err.println("finally clause in divide()");
    }
    System.out.println("Executing code after try block in divide()");
  }
}
    
First try block in divide() entered
Executing code after try block in divide()Arithmetic exception caught in divide()
Message in exception object:
  / by zero
Stack trace output:
java.lang.ArithmeticException: / by zero
  at MainClass.main(MainClass.java:8)
End of stack trace output
finally clause in divide()
    
6. 18. Exception
6. 18. 1. Error Handling
6. 18. 2. Types of Exceptions
6. 18. 3. Throwing an Exception from a Method
6. 18. 4. Write a catch block that handles java.lang.Exception
6. 18. 5. Handling Exceptions
6. 18. 6. Multiple catch Blocks
6. 18. 7. The finally Block
6. 18. 8. Exception Objects: stack trace
6. 18. 9. Defining Your Own Exceptions, Throwing Your Own Exception
www___.__j__a___va___2s._c_o___m
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.