This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concept on coding for Exception Handling

 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above code is of Anuraddha





Hi,

I am bit confused with throws and throw and how does it go with the method.

If anyone can help out with the code as given above.

Thank you.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a place to start.
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know about sun.java.tutorial.

But there i am not able to understand with proper code.

I can write the code for try-catch-finally.

But i am not able to get in throws, throw topic coding.

Thank you.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, a method can throw an exception when something wrong happens. That's what the "throw" keyword is for. You already know sun's tutorial, so you are already familiar with checked and unchecked exceptions. If you throw a checked exception from a method, you have to declare it, that's what the "throws" keyword is for. When you want to call this method, it tells you that this method throws TestException and that it should be delt with.

In the above example, the name must not be null. You check that name is not null. If it is null, you throw a TestException. If TestException is a checked exception, you have to declare that the sayHello method throws it. So you declare your method as "public String sayHello(String name) throws TestException".

The code calling this method will have to deal with this exception. So you should have something like :
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run the above code no doubt i get the output as :

HelloJohn

But when i modify as :




since i left the string intentionally blank i should get an error.

But i get the output as :

Hello.


Why no error message is printed ?
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because an empty string is different from a null value. Check again with System.out.println(b.sayHello(null));
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok .

Let me summarized my concept.

In the code we are checking that the programmer doesnt leave the string null.

So with the if condition we are testing and throwing the exc_name in the catch block to handle it.

So in our case TestException is a checked exception.

In short we need to declare alongwith the method signature as :



Unchecked exceptions no need to declare .

For this i can arrayOutOfBound exception.

Am i correct?
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this i can arrayOutOfBound exception.

Can you be more specific ?
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

the example of ArrayIndexOutOfBoundException as:



The above code try to get the value of a[3] but it throws an error since i have specified the size int[2] i.e. a[0], a[1]

So,
Exception in thread "main" java.lan ArrayIndexOutOfBoundException
at array.main(array.java:9)

My code compiles fine but creates an error at runtime.

So this is an unchecked exception.

am i correct?
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code compiles fine but creates an error at runtime. So this is an unchecked exception.

Better rephrase it: you don't have to catch it so this is an unchecked exception.

(no need to quote your own new messages )
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok

The exception which we catch in try-catch are CHECKED exceptions.

And the other we dont are UNCHECKED.

Correct ?
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Ok when the exception i am using in a method i have to declare with the Keyword "THROWS"

and if not in a method no need to declare as shown above.


Correct?
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following doesn't compile:



It doesnt compile because someMethod has not declare .

Correct ?

Ok one more query i know about this

throw new exception_name();

when we do this it goes in the catch block of that particular exception.

But---------------------

catch(MyException me)
{
throw me
}

Can we throw the exception object as shown above ?

 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you can, just correct your code and try it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic