Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

throw exceptions 1

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
hi all,
i'm new to java language and i'd like to know :
i have made a progam where i throw java.lang.NullPointerException this way :

Code:
public StringBuffer search(String SQLstring, StringBuffer sb_DATA) throws java.lang.NullPointerException {
try
   {
.....
   }
catch (NullPointerException d)
   {
	System.out.println("error : " + d + "\n");
	d.printStackTrace();
   }
}
the problem is that when a NullPointerException exist, i do have this result :

Code:
error : java.lang.NullPointerException

and nothing else (i mean i don't know where in my code is the error)


is there a way to know which code part is the reason of the error ? Best regards X-),
Elise
 

hi

1. you are catching "NullPointerException", but your method declare throwing "NullPointerException" ?! (I mean you don't need the "throws" part)

2. you can't know where exactly the code fails...

but you can trace that with a variable, I use to do that :

public StringBuffer search(String SQLstring, StringBuffer sb_DATA) throws java.lang.NullPointerException {

String step = null;

try
{

step = "describe step 1";
// some code....

step = "describe step 2";
// some code ...

...
}
catch (NullPointerException d)
{
System.out.println("error at step : " + step);
d.printStackTrace();
// and if the caller is expecting an exception
// you can add :
// throw d;
// or another type of exception... :)
}
}

3. catching "NullPointerException" seems weid, because when you are not sure you are suppose to verify an object is not "null" before using it... LOL

 

how are you doing to change the style of the fonts you use ??

manu0
 
manu : click on process TGML under this box, you use small tags like for smileys Best regards X-),
Elise
 
manu : click on process TGML under this box, you use small tags like for smileys

how do you test an object before to use it ? simply if(myObj == null) ??
Best regards X-),
Elise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top