Assume that a method named 'method1' contains code which may raise a non-runtime (checked) Exception.
What is/are the possible way(s) to declare this method so that it indicates that it expects the caller to handle that exception?
please explain the above question, i am confused about when to use throws and when to use throw?
About Question enthuware.ocajp.i.v7.2.1345 :
Moderator: admin
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1345 :
"throws" is used to declare that a method may throw a particular exception. For example, void m1() throws IOException; This means that the method m1 may throw IOException to the caller of this method at run time. i.e. if the caller calls this method, it may receive IOException.
"throw" is used to actually throw an exception. For example the code for method m1 may have something like this -
if(f == null) throw new FileNotFoundException();
If this statement is executed at runtime, the caller of m1 will get FileNotFoundException.
You should go through this topic from a good book before attempting mock questions because this is quite fundamental.
"throw" is used to actually throw an exception. For example the code for method m1 may have something like this -
if(f == null) throw new FileNotFoundException();
If this statement is executed at runtime, the caller of m1 will get FileNotFoundException.
You should go through this topic from a good book before attempting mock questions because this is quite fundamental.
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1345 :
thank you.
already read the ocajp7 but still a little confused
already read the ocajp7 but still a little confused
Who is online
Users browsing this forum: vs2013 and 7 guests