aspose file tools*
The moose likes Beginning Java and the fly likes declaring throws Exception after main() 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 "declaring throws Exception after main()" Watch "declaring throws Exception after main()" New topic
Author

declaring throws Exception after main()

john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
In my cert book, there is a question related to Exception, as follows:



My understaing is that a declaration is required if an Exception is thrown (therefore, if a 'throws' is used in the method). So, does the main not need to declare the Exception, just as other methods would need to?

Thanks in advance.

[edit]Add code tags. CR[/edit]
[ August 22, 2008: Message edited by: Campbell Ritchie ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by john smith:
My understaing is that a declaration is required if an Exception is thrown (therefore, if a 'throws' is used in the method). So, does the main not need to declare the Exception, just as other methods would need to?


Yes (assuming it's a checked exception you are throwing, which in this case it is). If you compiled your code, you would eventually get an error message to that effect, once you had fixed all the other errors.
[ August 22, 2008: Message edited by: Joanne Neal ]

Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Please quote which book it is, and Use Code Tags, which I have added so you can see how much easier your post is to read.

There are two "sorts" of Exception, checked and unchecked, and you can read lots more about them in the Java Tutorials. "Exception" is a checked one, so the compiler insists it be handled, or declared. Declaring an exception means you are telling the compiler this method doesn't handle it, and it will be passed back to wherever the method was called from. If you declare an Exception in main() you are passing it back to the JVM which (see Bruce Eckel Thinking in Java) prints a stack trace and closes the Thread the Exception was in.
That is exactly what would do; writing "throws Exception" is how you declare that this method might give rise to an Exception. You are correct; that class won't compile unless you add another try-catch or declare the Exception by writing "throws Exception" after the method signature.

I know this is only an example, but it is usually not a good idea to handle Exception itself in real life. You are usually better off dealing with a more specific Exception.
john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
Thank you all for the help. I am going to try to compile it, but it may be worth mentioning that the cert book does not give 'compilation fail' as the answer. It suggests "-ic mc mf of" would be printed.

This is why I was confused. Is there any reason for the printing instead of an compilation fails.

Many thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It indicates that there is a typographical error in the code you presented. The code you presented is not compilable because of several fairly large errors - missing end-of-statement, missing end-of-block, perhaps others. These errors happen at precisely the location that would determine
1) if you would need to throw exception
2) if "mc" would be added to the output.

From the code you posted it really is impossible to tell what the intention of the author was. Here is your code, reformatted to try and match braces and try/catch/finally blocks


Steve
john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
Thank you for the response. Indeed my layout was incorrect. I have now typed into Netbeans IDE and tested it. Here is a copy of the program and output.

I still don't understand why a 'throws' declaration is not needed?



//program start

public class Main {

/**
* @param args the command line arguments
*/
static String s = "-";
public static void main(String[] args) {
System.out.println("Hello world!");


try{
throw new Exception();
}catch (Exception e){
try{
try{ throw new Exception();
}catch(Exception ex){s += "ic ";}
throw new Exception();}
catch (Exception x) { s += "mc ";}
finally { s += "mf ";}
}finally {s += "of ";}
System.out.println(s);

}}

//program start


//************** ide output start **************

init:
deps-jar:
compile:
run:
Hello world!
-ic mc mf of
BUILD SUCCESSFUL (total time: 1 second)

//ide output end
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You should beware of code from certification books because a lot of them are "find-the-deliberate-error" puzzles.
Go back to your code and indent it properly (as per the JavaRanch style guide or similar, with every open { on a new line and every close } on its own line too. Use a decent text editor with bracket matching enabled (you can do it on NetBeans too) and put a line-end comment after it to show which block is ending there. For example the very last two }s should read like thisThen follow the course of execution with a pencil on a sheet of paper, and you see that every Exception is in fact caught.

You also see why we like proper indentation and code tags!
john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
Thanks to all for the help. I've got it. It is due to the fact that the Exceptions are being caught, and if not caught, it would need to be declared as 'throws'. I also tested it with the main() method.

Point taken about the indentaion and general advice. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Originally posted by john smith:
Thanks to all for the help.
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaring throws Exception after main()