4

If I have a class called Boat, and if I write :

class Boat throws Exception

Inside my class I am using try catch block to catch ArithmeticException for instance. What benefit there is to declare an exception versus not declaring an exception?

2
  • 1
    See this.
    – m0skit0
    Apr 14, 2013 at 12:53
  • 4
    A class does not throw exceptions. Only methods do.
    – Thilo
    Apr 14, 2013 at 12:55

4 Answers 4

3
  • A class does not throw exceptions. Only methods do.

  • For some exceptions (checked exceptions that may occur in your code and that you do not catch) the compiler forces you to declare them.

  • You never have to declare RuntimeExceptions (such as ArithmeticException), but you can. This serves as documentation.

  • You can declare checked exceptions that your code does not throw. This makes it future-proof if you might later want to throw them, and also allows for subclasses to do such.

  • When declaring exceptions, you can go broad/generic (throws Exception or even throws Throwable), but it is generally better to be more specific. That gives the people using your code a better idea of what to expect. The whole purpose of having these many specific Exception classes is to make it easier to handle exceptions appropriately (and have the compiler enforce the fact that someone at least thought about doing that).

2

First classes won't throw Exception, only methods will throw

check this Example

class A 
{
public  A() throws Exception
{
int k=5/0;
}

}

public class B 
{
public static void main(String[] args)
{
A a=new A();
}
}

When you run the above you'll get compile time error saying

Unhandled exception type Exception

because when ever methods throws exception , they are expected to handle by calling object, it is just like saying 'Hey somebody handle Exception', so the method calling it should handle the Exception , if they don't it will be a compile time error.

For the smooth execution of program, Exception thrown by method should be handle by calling method. Here is how you need to handle.

class A 
{
public  A() throws Exception
{
int k=5/0;
}

}

public class B 
{
public static void main(String[] args)
{
try{
A a=new A();
}
catch(Exception e)
{
System.out.println("caught  "+e);
}

}
}
3
  • so, the only benefit to declaring an exception to a method is to allow another class using that method to catch the exception? Please let me know.
    – Stranger
    Apr 14, 2013 at 13:46
  • 1
    @Stranger By throwing Exception you are specifying the, you method will throw the following Exceptions. It simply say that when you use this method, it will throw the following exception, you need to hanlde them. Example : Class.forName(str) will throw CNF Exception to specify that using this method will cause CNF Exception check method declaration here docs.oracle.com/javase/1.4.2/docs/api/java/lang/…
    – Pragnani
    Apr 14, 2013 at 14:02
  • 1
    "to allow another class using that method to catch the exception". More like to force the other class to think about what to do with the exception (catch or propagate).
    – Thilo
    Apr 14, 2013 at 23:13
2

Declaring Exception will allow your program to compile if any Exception can be determined by the compiler to "escape". It's primary purpose, though, is to document to the user of your method (and to their compilers) that it may throw an Exception.

As such, declaring Exception, vs declaring a more specific exception cause, is nullifying the benefits of the scheme -- cheating.

2

If you write something like that,

double x = 1/0; 
System.out.print("hey");

it will give you an exception and your code is going to stop, will not print hey. But if you encapsulate it with try/catch, it will not stop.

try{
    double x = 1/0; 
}catch(ArithmeticException){}
System.out.print("hey");

This will print hey.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.