This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Try,Catch & Throw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try,Catch & Throw" Watch "Try,Catch & Throw" New topic
Author

Try,Catch & Throw

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
what is difference between Try, Catch and Throw? Why and what is benefits using exception of the class in java?
Thanks,
Angela
Bryan Timmins
Ranch Hand

Joined: Mar 26, 2001
Posts: 48
That question requires a rather large response. I suggest
you go through the Sun path on this subject.
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html


<a href="http://www.bryantimmins.com" target="_blank" rel="nofollow">Bryan Timmins</a> <br />SCJP2
med bill
Greenhorn

Joined: Oct 27, 2000
Posts: 12
Try : A block that contains the code u want to monitor for exceptions. If a problem occurs during its execution, an exception is thrown.
catch: process the exception, it receives an argument as a parameter to handle the exception.
Finally: is optionnal and executed after a catch. JVM always execute Finally block.
Throw: explicitly generate an exception...
Best Regards.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks to all
Angela
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
What is difference between unchecked, checked and runtime exceptions?
Also, I have this following example for the method:


USING THROWN EXCEPTION METHOD:
public void writeList() throws IOException,
ArrayIndexOutOfBoundsException {

what is difference using throws exception and try& catch? Also,
is it must to use finally method when we use try & catch.

Thanks,
Angela

[This message has been edited by Marilyn deQueiroz (edited August 04, 2001).]
Kai Middleton
Greenhorn

Joined: Jul 27, 2001
Posts: 12
Anu wrote:

> I want to know if we don't use exception class, what will happened?
Basically you will have to acknowldege exceptions at some level in most programs. You can write a lot of simple programs that won't throw any exceptions. For instance, a fibonacci number generator

1 1 2 3 5 8 13 21 34 55 89 144 ...
It generally wouldn't throw any exceptions.
But if you do almost anything with IO, for instance, you will invoke methods that throw exceptions. If you wish to avoid using try, catch, etc. you can have methods like this:

Here by putting in

.. throws Exception

you are handling any possible exception that can be thrown. If all your methods do this then when you get an exception your program will just exit to the operating system and print a stack trace on the console (if it was run from a console).
This method isn't recommended for robust programs, but it's a way to prototype things if you don't want to worry about error conditions.

------------------
Kai M.


<I>Kai M.</I>
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks Kai,
So what I understood that:
if I used try & catch, then exception handlers will handle the exception and will not exit from the system.
if instead of try & catch,if i used throws exception, it will just exit from the OS and will print the message in java console and will terminate the program.

Please do verify this above
Thank again,
Angela
Originally posted by Kai Middleton:
[B]Anu wrote:
> I want to know if we don't use exception class, what will happened?
Basically you will have to acknowldege exceptions at some level in most programs. You can write a lot of simple programs that won't throw any exceptions. For instance, a fibonacci number generator
1 1 2 3 5 8 13 21 34 55 89 144 ...
It generally wouldn't throw any exceptions.
But if you do almost anything with IO, for instance, you will invoke methods that throw exceptions. If you wish to avoid using try, catch, etc. you can have methods like this:

Here by putting in
.. throws Exception
you are handling any possible exception that can be thrown. If all your methods do this then when you get an exception your program will just exit to the operating system and print a stack trace on the console (if it was run from a console).
This method isn't recommended for robust programs, but it's a way to prototype things if you don't want to worry about error conditions.

[/B]

William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

The "finally" is optional and doesn't need to be included when you do the try/catch.

if I used try & catch, then exception handlers will handle the exception and will not exit from the system.

When you use try & catch you can do anything you want within the catch. You can throw again, it is up to you. The try & catch ensure that the (possible) exception being generated is being handled - by you - anyway you want to handle it.

if instead of try & catch,if i used throws exception, it will just exit from the OS and will print the message in java console and will terminate the program.

The "throws" after a method signiture tells everyone that the listed exception may be generated by the method. It tells everyone that the exception is not being handled within the method and that anyone calling this method will need to try & catch this exception.

Please ignore post, I have no idea what I am talking about.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
so can I also use:
public void myMethod() throws IOException
{
// suppose getData() can throw an IOException
try
{
String x = getData();
}
catch(IOException e)
{}
}
If I can do this, then why I should use both i mean throws and try & catch block.
Thanks
Angela
Originally posted by christopher foran:
The "finally" is optional and doesn't need to be included when you do the try/catch.

if I used try & catch, then exception handlers will handle the exception and will not exit from the system.

When you use try & catch you can do anything you want within the catch. You can throw again, it is up to you. The try & catch ensure that the (possible) exception being generated is being handled - by you - anyway you want to handle it.

if instead of try & catch,if i used throws exception, it will just exit from the OS and will print the message in java console and will terminate the program.

The "throws" after a method signiture tells everyone that the listed exception may be generated by the method. It tells everyone that the exception is not being handled within the method and that anyone calling this method will need to try & catch this exception.

William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

(You don't have to include the text of the entire previous post, we can see that already by scrolling up.)
There seems to be a basic misunderstanding you are having, and I am not exactly sure what it is. You seem to be asking the same questions over. Did you checkout the link that Bryan included. I know that it is difficult to have a meaning full conversation when you can't talk to someone directly.
Let me make sure one basic point is clear. Throw and Throws are not the same. Throw generates an exception and Throws tells everone that a method can return an exception.
Now your exact question was:
If I can do try & catch and throws why would I use both?
And there is more than one answer.
Using the code you had in your post you would not use throws. Why? Because the exception was handled by the catch, and not rethrown.
If on the other hand your code did this

Than the compiler would insist that you use the "throws". The "throws" lets anyone calling this mehod know that an exception can be returned.
Hopefully that clears it up just a little.
Please post back it isn't clear or you have other questions, thanks.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks to all,
I got it...
Thanks again,
Angela
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try,Catch & Throw
 
Similar Threads
How to throw exceptipn through servlet.
exceptions????
Exceptiopn handling
What is catch/throw ?
try catch error handling