OCA Java SE 8 Exception - Java Exception in action








Calling Methods That Throw Exceptions

When you're calling a method that throws an exception, the rules are the same as within a method. Do you see why the following doesn't compile?

class Exception1 extends Exception {} 

public class Rectangle { 
  public static void main(String[] args) { 
    myMethod();// DOES NOT COMPILE 
  } 
  private static void myMethod() throws Exception1 { 
  } 
} 

The problem is that Exception1 is a checked exception. Checked exceptions must be handled or declared.

The code would compile if we changed the main() method to either of these:

public static void main(String[] args) throws Exception1 {// declare exception 
  myMethod(); 
} 

or

public static void main(String[] args) { 
  try { 
    myMethod(); 
  } catch (Exception1 e ) {// handle exception 
    System.out.print("sad rabbit"); 
  } 
} 

myMethod() didn't actually throw an exception and it just declared that it could.

This is enough for the compiler to require the caller to handle or declare the exception.

Do you see the issue here?

public void bad() { 
  try { 
      myMethod(); 
  } catch (Exception1 e ) {// DOES NOT COMPILE 
      System.out.print("exception"); 
  } 
} 
public void good() throws Exception1 { 
     myMethod(); 
} 
private static void myMethod() { } 

Java knows that myMethod() can't throw a checked exception.

In comparison, good() is free to declare other exceptions.





Subclasses

When a class overrides a method from a superclass or implements a method from an interface, it's not allowed to add new checked exceptions to the method signature.

For example, this code isn't allowed:

class Exception1 extends Exception { } 
class Shape { 
  public void print() { } 
} 
class Rectangle extends Shape { 
  public void print() throws Exception1 { } // DOES NOT COMPILE 
} 

Java knows print() isn't allowed to throw any checked exceptions because the superclass Shape doesn't declare any.

A subclass is allowed to declare fewer exceptions than the superclass or interface. This is legal because callers are already handling them.

class Shape { 
  public void print() throws Exception1 { } 
} 
class Rectangle extends Shape { 
  public void print()  { } 
} 

A class can declare a subclass of an exception type.

The superclass or interface has already taken care of a broader type.

class Shape { 
  public void print() throws Exception { } 
} 
class Rectangle extends Shape { 
  public void print() throws Exception1 { } 
} 

Rectangle could declare that it throws Exception directly, or it could declare that it throws a more specific type of Exception. It could even declare that it throws nothing at all.

This rule applies only to checked exceptions. The following code is legal because it has a runtime exception in the subclass's version:

class Shape { 
  public void print() { } 
} 
class Rectangle extends Shape { 
  public void print() throws IllegalStateException { } 
} 

We can declare new runtime exceptions in a subclass method since the declaration is redundant.

Methods can throw any runtime exceptions without mentioning them in the method declaration.





Printing an Exception

There are three ways to print an exception. You can let Java print it out, print just the message, or print where the stack trace comes from.

5: public static void main(String[] args) { 
6:    try { 
7:     print(); 
8:    } catch (Exception e) { 
9:      System.out.println(e); 
10:     System.out.println(e.getMessage()); 
11:     e.printStackTrace(); 
12:   } 
13: } 
14: private static void print() { 
15:   throw new RuntimeException("cannot print"); 
16: } 

Java prints out by default: the exception type and message in the following code.

System.out.println(e); 

The following line shows just the message.

System.out.println(e.getMessage()); 

The rest shows a stack trace.

e.printStackTrace();