PDA

View Full Version : Excpetions , Multiple Exceptions , how ??



saneha
04-24-2011, 06:54 PM
Sir, I got a question , how to throw multiple exceptions form a method , and how we could know that , on a particular event , a specific kind of exception will be generated ,

for example we take input form user , and user don't write anything , what kind of the exception will be generated from here. and in the same method there is a chance of another exception like we try to open a file which don't exist , in this case there will be a total chance of two exceptions, One will be IO related, what will be the other ?????. This is just a case I think in different cases there will be different kind of exceptions. How can we know all them??
e.g.
public void method()
{
String str ;// A Null point exception
//assume user don't enter any input in the field ...there will be another exception.
str =JOptionPane.showInputDialog(null, "Please enter input. ");
dobule dAge ;
dAge= Double.parseDouble(JoptionPane.showInputDialog(nul l, "Please enter your age. "))//assume user enter his age in the form of String "like twenty instead of 20”. Here, another exception will be generated.
}

Now my question is how can we throw all these exceptions , how can we figure out that what kind of exception will be generated and how can we catch them ????


Thanks ….

Vuhelper
04-24-2011, 06:55 PM
You can throw multiple exceptions as follows

public void method() throws IOException, NullPointerException //so on you can add as many as you want or require and this will be handled in caller //function
{
String str ;// A Null point exception
//assume user don't enter any input in the field ...there will be another exception.
str =JOptionPane.showInputDialog(null, "Please enter input. ");
double dAge ;
dAge= Double.parseDouble(JoptionPane.showInputDialog(nul l, "Please enter your age. "))//assume user enter his age in the form of String "like twenty //instead of 20”. Here, another exception will be generated.
}

IOException can catch all type of exceptions.