Error Handling : Exception « Development « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Development » Exception 
6. 23. 1. Error Handling
  1. You can isolate code that may cause a runtime error using the try statement.
  2. try statement normally is accompanied by the catch and the finally statements.
  3. In case of an error, Java stops the processing of the try block and jump to the catch block.
  4. In the catch block, you can handle the error or notify the user by 'throwing' a java.lang.Exception object. Or you can re-throw the exception or a new Exception object back to the code that called the method.
  5. If a thrown exception is not caught, the application will stop abruptly.
  6. In the finally block, you write code that will be run whether or not an error has occurred.
  7. The finally block is optional.
  8. You can have more than one catch block. This is because the code can throw different types of exceptions.
  9. If the type of exception thrown does not match the exception type in the first catch block, the JVM goes to the next catch block and does the same thing until it finds a match.
  10. If no match is found, the exception object will be thrown to the method caller.
  11. If the caller does not put the offending code that calls the method in a try block, the program crashes.

This is the syntax of the try statement.

try {
         [code that may throw an exception]
     catch (ExceptionType-e) {
         [code that is executed when ExceptionType-is thrown]
     } [catch (ExceptionType-e) {

         [code that is executed when ExceptionType-is thrown]
     }]
       ...
     } [catch (ExceptionType-n e) {
         [code that is executed when ExceptionType-n is thrown]
     }]
     [finally {
         [code that runs regardless of whether an exception was thrown]]
     }]
6. 23. Exception
6. 23. 1. Error Handling
6. 23. 2. Types of Exceptions
6. 23. 3. Java's Unchecked RuntimeException Subclasses
6. 23. 4. Java's Checked Exceptions Defined in java.lang
6. 23. 5. Throwing an Exception from a Method
6. 23. 6. Write a catch block that handles java.lang.Exception
6. 23. 7. Handling Exceptions
6. 23. 8. Multiple catch Blocks
6. 23. 9. The finally Block
6. 23. 10. Exception Objects: stack trace
6. 23. 11. Defining Your Own Exceptions, Throwing Your Own Exception
6. 23. 12. Demonstrate exception chaining.
6. 23. 13. Getting the Stack Trace of an Exception
6. 23. 14. Put printStackTrace() into a String: redirect the StackTrace to a String with a StringWriter/PrintWriter
6. 23. 15. Check whether given exception is compatible with the exceptions declared in a throws clause
6. 23. 16. Convert an exception to a String with full stack trace
6. 23. 17. Get Deepest Throwable
6. 23. 18. Get the stack trace of the supplied exception.
6. 23. 19. Is Checked Exception
6. 23. 20. Locates a particular type of exception
6. 23. 21. Make a string representation of the exception
6. 23. 22. Print all of the thread's information and stack traces
6. 23. 23. Return stack trace from the passed exception as a string
6. 23. 24. Returns the root cause of an exception
6. 23. 25. Create a new Exception, setting the cause if possible.
6. 23. 26. Returns the output of printStackTrace as a String.
6. 23. 27. This program creates a custom exception type.
6. 23. 28. Utility methods for dealing with stack traces
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.