• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Concept on coding for Exception Handling

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a place to start.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For this i can arrayOutOfBound exception.


Can you be more specific ?
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you can, just correct your code and try it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic