Java chain exception

java

i have question regarding chain exception

try{ } catch(Exception e) { throw new SomeException(); } 

if i do like this
my eclipse will prompt error at line throw new SomeException();
stating "unhandled exception"
and i must put something like

try{ } catch(Exception e) {
                            try{ throw new SomeException(); } catch(Exception e){} 
                           }

why must do like this
because tutorial that i read .example http://java.sys-con.com/node/36579 , does not have to do this

Best Solution

You'll need to declare that the method throws another exception, if the exception is a checked exception.

("The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes." -- Java Language Specification, Second Edition, Section 11.2)

For example, rather than:

void someMethod {
    try {
        // Do something that raises an Exception.
    } catch (Exception e) {
        throw new SomeException();    // Compile error.
    }
}

A throws needs to be added to the method declaration:

void someMethod throws SomeException {
    try {
        // Do something that raises an Exception.
    } catch (Exception e) {
        throw new SomeException();    // No problem.
    }
}