Turning off Checked exceptions : Exceptions « Language Basics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
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 » Language Basics » ExceptionsScreenshots 
Turning off Checked exceptions

//: c09:TurnOffChecking.java
// "Turning off" Checked exceptions.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
import java.io.*;

class WrapCheckedException {
  void throwRuntimeException(int type) {
    try {
      switch(type) {
        case 0throw new FileNotFoundException();
        case 1throw new IOException();
        case 2throw new RuntimeException("Where am I?");
        defaultreturn;
      }
    catch(Exception e) { // Adapt to unchecked:
      throw new RuntimeException(e);
    }
  }
}

class SomeOtherException extends Exception {}

public class TurnOffChecking {

  public static void main(String[] args) {
  

    WrapCheckedException wce = new WrapCheckedException();
    // You can call f() without a try block, and let
    // RuntimeExceptions go out of the method:

    wce.throwRuntimeException(3);
    // Or you can choose to catch exceptions:

    for(int i = 0; i < 4; i++)
      try {
        if(i < 3)
          wce.throwRuntimeException(i);
        else
          throw new SomeOtherException();
      catch(SomeOtherException e) {
          System.out.println("SomeOtherException: " + e);
      catch(RuntimeException re) {
        try {
          throw re.getCause();
        catch(FileNotFoundException e) {
          System.out.println(
            "FileNotFoundException: " + e);
        catch(IOException e) {
          System.out.println("IOException: " + e);
        catch(Throwable e) {
          System.out.println("Throwable: " + e);
        }
      }
  }
///:~


           
       
Related examples in the same category
1. Illustrate various Exceptions Illustrate various Exceptions
2. Experience exceptionsExperience exceptions
3. StackTrace
4. What happens if a method declares an unchecked exception?
5. Simple demo of exceptionsSimple demo of exceptions
6. Simple demo of exceptions, with finally clauseSimple demo of exceptions, with finally clause
7. ThreadBasedCatcher - Demonstrate catching uncaught exceptions
8. Exception CatcherException Catcher
9. Demonstrates exception chainingDemonstrates exception chaining
10. Finally is always executedFinally is always executed
11. Demonstrating the Exception MethodsDemonstrating the Exception Methods
12. Further embellishment of exception classesFurther embellishment of exception classes
13. The finally clause is always executedThe finally clause is always executed
14. Your own Exception classYour own Exception class
15. Catching exception hierarchiesCatching exception hierarchies
16. How an exception can be lost
17. Exception in main method
18. Ignoring RuntimeExceptionsIgnoring RuntimeExceptions
19. Demonstrating fillInStackTrace()Demonstrating fillInStackTrace()
20. Rethrow a different object from the one that was caughtRethrow a different object from the one that was caught
21. Inheriting your own exceptionsInheriting your own exceptions
22. Overridden methods may throw only the exceptions
23. Throw Exception OutThrow Exception Out
w_w___w__.__ja_va__2___s___.__com_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.