The Artima Developer Community
Sponsored Link

Java Answers Forum
exception handling

3 replies on 1 page. Most recent reply: Dec 12, 2003 8:11 PM by maxima

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
maxima

Posts: 15
Nickname: maxima
Registered: Nov, 2003

exception handling Posted: Dec 12, 2003 5:56 AM
Reply to this message Reply
Advertisement
hi ...all i know exception checking is done on a piece of block.through try/catch block..the catch statement give us the message.

but i was surprised to see a java program like below

import java.io.*;

class WriteTextFile
{

public static void main ( String[] args ) throws IOException
{
String fileName = "reaper.txt" ;

FileWriter writer = new FileWriter( fileName );

writer.write( "Behold her, single in the field,\n" );
writer.write( "Yon solitary Highland Lass!\n" );
writer.write( "Reaping and singing by herself;\n" );
writer.write( "Stop here, or gently pass!\n" );

writer.close();
}
}


who will catch exception and give us warning???

can method throw exception???

i am a newbie to java


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: exception handling Posted: Dec 12, 2003 12:02 PM
Reply to this message Reply
Yes, a method can throw exception. In your sample program main() method is meant to throw exception if it happens at runtime. Simple rule is that:

If you use a method m1() inside a method m2() and m1()throws a checked exception, then either you place m1() inside try-catch and handle the execption thrown by m1() or m2() should declare that it throws that execption.

A checked exception must be caught from the place of its use to somewhere up in hierarchy.

So, your main() method declaration is fine.

s

Posts: 23
Nickname: codemonkey
Registered: Nov, 2003

Re: exception handling Posted: Dec 12, 2003 12:42 PM
Reply to this message Reply
If you wanted to do anything meaningful you would wrap
the code in a try/catch block as you have no control over the
caller of the method. In my experience ,it is rare to see the main method throw an exception.
The point of exception handling is to actually do something meaningful or die gracefully.

Monkey

maxima

Posts: 15
Nickname: maxima
Registered: Nov, 2003

Re: exception handling Posted: Dec 12, 2003 8:11 PM
Reply to this message Reply
thanks..

Flat View: This topic has 3 replies on 1 page
Topic: compilation error Previous Topic   Next Topic Topic: Font and Applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use