About Exceptions in Java

Categories: Java; Tagged with: ; @ May 20th, 2012 17:22

Exceptions in Java:

                                      Object
                                     Throwable
                         Error                    Exception

                                           RuntimeException …..   

There are two types Exception:

1. Checked Exception , we need to declare ‘throws’ in mehtod if there maybe exception be  thrown out, and must be catch or throw it if we use the method.
2. UnChecked exception(RuntimeExcepiton), will be thorwed in runtime, no need to declare them in the method.

1. UnChecked Exception:

public class TestException {
	public void test() throws NullPointerException{
		System.out.println("Test  OK");
	}
	
	public static void main(String[] args) {
		new TestException().test();
	}
}

output: “Test OK”

2. Checked Exception:

public class TestException {
	public void test() throws IOException{
		System.out.println("Test  OK");
	}
	
	public static void main(String[] args) {
		new TestException().test(); // Can not compile: surround with try/catch or add throws declaration
	}
}

3. Overriding:

Access modifier can be enlarged, but exception only can be less or specialized.

package scjp.liguoliang.com;

import java.io.FileNotFoundException;
import java.io.IOException;

public class TestException {
	public void test() throws IOException{
		System.out.println();
	}

}

class TestExceptionSub extends TestException {
	public void test() throws FileNotFoundException{ // FileNotFoundException extends IOException
		
	}
}

This can work fine, but if we let TestExceptionSub to implement an Interface, like:

package scjp.liguoliang.com;

import java.sql.SQLException;

public interface ITestException {

	void test() throws  SQLException;
}
package scjp.liguoliang.com;

import java.io.FileNotFoundException;
import java.io.IOException;

public class TestException {
	public void test() throws IOException{
		System.out.println();
	}

}

class TestExceptionSub extends TestException implements ITestException{
	public void test() throws FileNotFoundException{ //Can not compile!
		
	}
}

Multiple markers at this line
    – overrides scjp.liguoliang.com.TestException.test
    – Exception FileNotFoundException is not compatible with throws clause in
     ITestException.test()

 

for test() in TestExceptionSub , If we want to throw any checked exception, we need get the sub class from ‘IOException’ and ‘SQLException’., or just remove throws declaration.

but for un-checked exception, you can throw any, and no matter with the super class declaration.

4. Catch sequence: the specification exception need catched before it’s super exception, otherwise:
“Unreachable catch block for Exception. It is already handled by the catch block for Throwable”

	public static void main(String[] args) {
		try{
			throw new SQLException();
		}catch (SQLException e) {
			System.out.println("SQLException catched!");
			return;
		}catch (Throwable e) {
			System.out.println("Exception Catched!");
			return;
		}
		finally {
			System.out.println("Finally");
		}
	}

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.