Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

throw exception

Hi,

public class Exceptions {

	public void test() throws NullPointerException{
		Exception e=new Exception("exception occured");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			System.out.println("next");
			// } catch (NullPointerException e) {

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		// System.out.println("hi");
	}

}

Open in new window


why above code giving compile time error saying

Unhandled exception type Exception


please advise

is catch has to match the test() throws exception type or other way. What is best practice. please advise
Avatar of dpearson
dpearson

	public void test() throws NullPointerException{
		Exception e=new Exception("exception occured");
		throw e;
	}

Open in new window


Exception is a checked exception - so needs to be caught or added to the "throws" list.  Just like you thought.

Doug
as by above comment, and as I comments in your other question, you need to add the exception to the list in the declaration of the function:

public void test() throws NullPointerException, Exception {

then that compile error will go away.
see this page for the reference manual:
https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html
Avatar of gudii9

ASKER

public class Exceptions {

	public void test() throws NullPointerException,Exception{
		Exception e=new Exception("exception occured");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			System.out.println("next");
			// } catch (NullPointerException e) {

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		// System.out.println("hi");
	}

}

Open in new window


above has no error
Avatar of gudii9

ASKER

public class Exceptions {

	public void test() throws Exception,NullPointerException{
		Exception e=new Exception("exception occured");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			System.out.println("next");
			// } catch (NullPointerException e) {

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		// System.out.println("hi");
	}

}

Open in new window


above also no error.

so issue with order?
Guddi9 wake up. Why are you throwing NPE as well as Exception?
These 3:

public void test() throws Exception,NullPointerException{
public void test() throws NullPointerException, Exception{
public void test() throws Exception {

are all equivalent.

Order doesn't matter and NullPointerException extends Exception, so throws Exception covers all cases here.

Doug
Avatar of gudii9

ASKER

Order doesn't matter and NullPointerException extends Exception, so throws Exception covers all cases here.

i thought reverse somehow as below

Order doesn't matter and NullPointerException  is sub class of Exception, so NullPointerException Exception covers all cases here.

how to remember this?
To understand: in the functions definition you only declare what exceptions the function may raise, for compile checks.
Inside the function body you can then throw your exceptions as needed and as declared.
Avatar of gudii9

ASKER

in the functions definition you only declare what exceptions the function may raise, for compile checks.

you mean fucntion definition is below right

public void test() throws Exception,NullPointerException{



Inside the function body you can then throw your exceptions as needed and as declared.
you mean below highlighted right

public void test() throws NullPointerException{
            Exception e=new Exception("exception occured");
            throw e;
      }
how to remember this?

Exception is the top of the tree.  It's as easy as that.

So you will generally either see a method like this:

public void myMethod throws AnException, AnotherException

or

public void myMethod throws Exception

You won't run into too many others.  So it's not that hard to remember.

Doug
Avatar of gudii9

ASKER

     public void test() throws NullPointerException{
            Exception e=new Exception("exception occured");
            throw e;
      }

Select all
 
Open in new window

Exception is a checked exception - so needs to be caught or added to the "throws" list.  Just like you thought.



so needs to be caught or added to the "throws" list

i am catching it right in my original code as below?
public class Exceptions {

	public void test() throws NullPointerException{
		Exception e=new Exception("exception occured");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			System.out.println("next");
			// } catch (NullPointerException e) {

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		// System.out.println("hi");
	}

}

Open in new window

Unhandled exception type Exception

i see  i should have changed throws to Exception not NPE
as test() method throw Exception not NPE



so when i changed to Exception no more compiler error as below

public class Exceptions2 {

	public void test() throws Exception{
		Exception e=new Exception("exception occured");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			System.out.println("next");
			// } catch (NullPointerException e) {

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		// System.out.println("hi");
	}

}

Open in new window


so this lead to one other question

public void test() [u]throws [/u]Exception{
		Exception e=new Exception("exception occured");
		[b]throw e;[/b]
	}

Open in new window

so everytime if a method throw something here
throw e(as bolded)
method should has throws(as underlined above)
i mean i can have a throw without throws or vice versa?
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

if you handle/catch all errors in the function, then you don't need to do anything.
but as soon a you explicitly throw exceptions, you need to implement throws also
if throw then have throws also and whoever calling must catch it right?
You don't HAVE to catch anything. You can let it propogate if you want.

You only catch if you want to HANDLE it.
Avatar of gudii9

ASKER

Exception is a checked exception - so needs to be caught or added to the "throws" list.  Just like you thought.
needs to be caught means needs to be caught in test() method not within calling code area?
i was not earlier clear now i am clear it needs to be caught within test() method
Avatar of gudii9

ASKER

You don't HAVE to catch anything. You can let it propogate if you want.

You only catch if you want to HANDLE it.

that makes sense.

how to decide what is best place to handle or what is not best place to handle but rather let it propagate?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

E.g.
   to_go_shopping() {
        get_in_car() ;
        drive_to_shop() ;
        buy_food() ;
   }

get_in_car() {
    find_car() ;
    open_door() ;
    put_in_keys() ;
}

If put_in_keys() finds you have no keys - it should propagate the exception up - because when you go to put keys into your car and you don't have them, it's not the correct level to decide what to do.

You probably want to propagate that error all the way up through the to_go_shopping() method - so you can decide that you should go look for your keys.

Does that make sense?

it makes sense. where i cana read and know more about these real life practical example to understand concepts deeply and clearly
it makes sense.

Excellent.

where i cana read and know more about these real life practical example to understand concepts deeply and clearly
Sorry - I don't know where you can see more examples like this.  Most professional developers don't know this rule :)

Doug