-4

I know javac forces the programmer to handle checked exceptions which need to be thrown by the method or handled using try-catch/finally. And it will not stop programmer from throwing an unchecked exception.

I also know why we do try-catch/finally and understood why java made certain set of exceptions checked.

But I am not able to understand the below:

Q1) Why should a method "throws" an exception. What is the benefit we get out of it? I think, either a method throws or does not throw using "throws" same thing is happening!!!

Q2) What happens internally when a method "throws" an exception?

Q3) I am thinking "Errors" are classified differently from "Exceptions" only to highlight them as "there won't be much from programmer's side to do when they occur" but we can do "throws" "try-catch/finally" for them as usual and everything else is also as same as with "Exception"*s. Please correct me if I am wrong.

Please help me. Thank you in advance.

Venkataswamy
  • 1,019
  • 7
  • 7
  • exceptions allows a better interface to handle errors, if you don't throw exceptions you will have to rely on the old methods of having the function return error values, this method is not efficient because then the code needs to check the return value of a function to see if an error happens. its much clearer when an exception is thrown when the error happens because then it can be handled differently. – DevZer0 Feb 12 '14 at 04:13
  • 5
    https://www.google.com/search?q=exceptions+java and especially http://docs.oracle.com/javase/tutorial/essential/exceptions/ – aliteralmind Feb 12 '14 at 04:13

1 Answers1

1

K here we go :

Q1) Why should a method "throws" an exception. What is the benefit we get out of it?

A method should mention that it throws an exception when it can't handle the exception that it throws (the method must specify this behavior so that callers of the method can guard themselves against that exception ).

Q2) What happens internally when a method "throws" an exception?

When a method throws an exception the control is transferred to the caller of the method and its whether the caller has necessary catch block to handle the exception thrown by the method. If the caller has a catch block to deal with the exception the exception will be dealt and the program continues. If the caller doesn't have necessary means to handle the exception that's been thrown at it, the exception will be handled by the default exception handler.

For your 3rd question read this block taken from the complete reference book :

All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct branches. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. This is also the class that you will subclass to create your own custom exception types. There is an important subclass of Exception, called RuntimeException. Exceptions of this type are automatically defined for the programs that you write and include things such as division by zero and invalid array indexing.

The other branch is topped by Error, which defines exceptions that are not expected to be caught under normal circumstances by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error. This chapter will not be dealing with exceptions of type Error, because these are typically created in response to catastrophic failures that cannot usually be handled by your program.

iamyogish
  • 2,372
  • 2
  • 23
  • 40
  • Thank you. I understood this, please confirm: (1) A method has to throw a checked exception to tell the compiler that "I know it's important to handle but I am leaving that responsibility to the caller" (2) We can cheat compiler on handling checked exceptions by using empty catch/finally blocks (3) A method throws an exception( checked or unchecked ) to let the caller handle it better – Venkataswamy Feb 12 '14 at 05:58
  • (4) Errors is just another category to differentiate from other two types "RuntimeExceptions" and "CheckedExceptions". I am sure we can throw or handle Errors but I don't understand why they say "Errors not expected to caught"....I believe there will always be need to do some cleanup operations before exit the program even in case of serious issues like Errors. – Venkataswamy Feb 12 '14 at 05:59
  • Your (1) point is correct. Coming to the (2) point using empty catch blocks is bad practice because you'll be swallowing the exception.(3) point method throws an checked exception why i'm saying this coz all unchecked exceptions are runtime exceptions and the compiler won't force you to check for it in a try/catch clause. – iamyogish Feb 12 '14 at 06:16
  • (4) point in simple words in java ERROR refers to problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. – iamyogish Feb 12 '14 at 06:16
  • Thank you. If that's the case, Why Java has Error class and its subclasses in its library. Purpose? – Venkataswamy Feb 12 '14 at 09:23