aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Concept on coding for Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Concept on coding for Exception Handling" Watch "Concept on coding for Exception Handling" New topic
Author

Concept on coding for Exception Handling

dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Here is a place to start.


[My Blog]
All roads lead to JavaRanch
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Sep 12, 2008
Posts: 90
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Because an empty string is different from a null value. Check again with System.out.println(b.sayHello(null));
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

For this i can arrayOutOfBound exception.

Can you be more specific ?
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Sep 12, 2008
Posts: 90
Ok

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

And the other we dont are UNCHECKED.

Correct ?
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90




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

Joined: Sep 12, 2008
Posts: 90
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

Joined: Oct 30, 2009
Posts: 504
yes you can, just correct your code and try it.


SCJP 1.6 96%
 
 
subject: Concept on coding for Exception Handling