gudii9
asked on
throw exception
Hi,
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
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");
}
}
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
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
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
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");
}
}
above has no error
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");
}
}
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,NullPointerExcep tion{
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
public void test() throws Exception,NullPointerExcep
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
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.
Inside the function body you can then throw your exceptions as needed and as declared.
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,NullPointerExcep
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;
}
Yes
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
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");
}
}
Unhandled exception type Exceptioni 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");
}
}
so this lead to one other question
public void test() [u]throws [/u]Exception{
Exception e=new Exception("exception occured");
[b]throw e;[/b]
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
if you handle/catch all errors in the function, then you don't need to do anything.if throw then have throws also and whoever calling must catch it right?
but as soon a you explicitly throw exceptions, you need to implement throws also
You don't HAVE to catch anything. You can let it propogate if you want.
You only catch if you want to HANDLE it.
You only catch if you want to HANDLE it.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 clearlySorry - I don't know where you can see more examples like this. Most professional developers don't know this rule :)
Doug
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