aspose file tools*
The moose likes Beginning Java and the fly likes why Throws to main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why Throws to main method" Watch "why Throws to main method" New topic
Author

why Throws to main method

kedar parundekar
Ranch Hand

Joined: May 10, 2006
Posts: 40
/*code :------------------->*/

class DemoExcep2
{
int a,b,res;
public void disp() throws Exception
{
a=10;
b=0;
res=a/b;
System.out.println(res);
}

public static void main(String[] args)
{

DemoExcep2 obj =new DemoExcep2();
obj.disp();



}
}
/**************************************/
Q > if i run above program then I have Compile time error

DemoExcep2.java:15: unreported exception java.lang.Exception; must be caught or
declared to be thrown
obj.disp();

Why ?
^
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Because you're calling disp() which throws an Exception. This is a checked exception which have to either catch, or rethrow.
You could add a try/catch block into your main method.


[My Blog]
All roads lead to JavaRanch
Ellie Barnes
Greenhorn

Joined: Nov 20, 2005
Posts: 5
Because your method throws an exception, that exception must be dealt with by the calling method (in this case your main method). I'm not really sure what you want to do with this method, so this will just print you the error message associated with the exception:


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

The Java Tutorial on Sun's website explains exactly what exceptions are, how to use them and what the difference between checked and unchecked exceptions is.

Handling Errors Using Exceptions

You are required to handle checked exceptions with a try / catch or throw them on to the calling method.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kaush Kane
Ranch Hand

Joined: May 22, 2006
Posts: 37
I have one general question on this topic.
Normally it is said that the code which is likely to throw exception shuould be inside the try block.

The question I have is: In case of Java Runtime Exceptions it is not possible everytime to know which part of code wud throw an exception. In such case how to decide that which part of the code shud be in try block.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That's a tough one. For example you can get NullPointer almost anywhere but you wouldn't wrap every method call in a try-catch block.

I wrote a little web server with a method at the top of the call stack that attempts to catch and display everything. If the server can survive the error, it goes on running. If it can't, well, it can't and we haven't lost much in trying.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
If this is just a bit of test code, I find it slightly easier to have the main throw an exception. YMMV:


There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Kaush Kane:
I have one general question on this topic.
Normally it is said that the code which is likely to throw exception shuould be inside the try block.

The question I have is: In case of Java Runtime Exceptions it is not possible everytime to know which part of code wud throw an exception. In such case how to decide that which part of the code shud be in try block.


That's only partially true. Exceptions a method may throw should be documented regardless of whether or not they are checked or unchecked. If you and your colleagues do this then all that is needed to find out what can be thrown is a check of the documentation. In that scenario it's purely a matter of whether or not you want to handle the exception there. If you can handle it and want to handle it there, then do so. If you can guarantee the exception will never occur that's antoher option. If you do want to handle it, then handle it.

Working with legacy code or APIs outside of your control is much more difficult if the developer did not document exceptions well. In that case I'm not sure there's much you can do except deal with it on a case by case basis. As Stan alluded to some type of safety net to catch anything that slips through which should not take down the entire application is also a good idea. Hopefully it never reaches that point, but better to have the problem logged for you to deal with than to have the entire application die for something trivial.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why Throws to main method