Exceptions again !!!!
class DataAccess
{
public DataAccess(Connection connection)
{
// I have traeted the exception in the class DB
if(connection==null)
{
DB db=new DB();
connection=db.connect();
this.connection=connection;
}
else
{
this.connection=connection;
}
try
{
connection=null;/// I did this to generate the error
sqlInsertUser= connection.prepareStatement("INSERT INTO userlist "+
"(id_user,login_user,pass_user ) VALUES(?,?,md5(?) ) ");
}
catch (SQLException ex)
{
logger.log(Level.SEVERE,"SQL Error See Log File For More Details",ex);
ex.printStackTrace();
}
}
}
class CallObjectFomHere
{
//constructor and other declaration
DataAccess obj =new DataAccess(connection);
System.out.println("the program does not reach this line if I dont add a try catch ? ");
// other method will not be reached also
obj.insertUser();
}
my question ,is this the best way to treat the error ?
why can't the programme execute the last 2 lines in CallObjectFomHere ?
It can. why shouldn't it?
And whether it's the best way: the best way to do what?
<the program does not reach this line if I dont add a try catch ? "
I'm not sure if your saying that when you don't have a try catch you don't get here or what but if you get an error and you don't catch it, the program will end, but if you catch an error or exception then the program breaks out of the try statement does the catch statement and moves on that help?>
> It can. why shouldn't it?
if I don't surround DataAccess obj = new DataAccess(connection);
with a try catch then I will not reach the 2 lines after ,I have tested it
>
> And whether it's the best way: the best way to do
> what?
is it better maybe to let the constructor of the class DataAccess throws sqlException ?
> <the program does not reach this line if I dont add a
> try catch ? "
>
> I'm not sure if your saying that when you don't have
> a try catch you don't get here or what but if you get
> an error and you don't catch it, the program will
> end, but if you catch an error or exception then the
> program breaks out of the try statement does the
> catch statement and moves on that help?
yes ,I get it now thank you
edit start here
infact my pb is that I'm not catching the error .
but the strange thing is when I press the button that do this action it will excute again and generate the error again ?
so in way the program did not stop ?
Message was edited by:
linuxchild
<but the strange thing is when I press the button that do this action it ><will excute again and generate the error again ?
><so in way the program did not stop ?
is the button part of your program? or are you executing it again?>
the program will move to the next line if I add catch nullPointerException
class DataAccess
{
public DataAccess(Connection connection)
{
// I have traeted the exception in the class DB
if(connection==null)
{
DB db=new DB();
connection=db.connect();
this.connection=connection;
}
else
{
this.connection=connection;
}
try
{
connection=null; /// I did this to generate the error
sqlInsertUser= connection.prepareStatement("INSERT INTO userlist "+
"(id_user,login_user,pass_user ) VALUES(?,?,md5(?) ) ");
}
catch (SQLException ex)
{
logger.log(Level.SEVERE,"SQL Error See Log File For More Details",ex);
ex.printStackTrace();
}
//I added this line to be able to move to the next line
catch(NullPointerException ex)
{
System.out.println("im here2");
ex.printStackTrace();
}
}
}
so would you treat your errors in the same way ?
> <but the strange thing is when I press the button
> that do this action it <will excute again and
> generate the error again ?
> <so in way the program did not stop ?
>
> is the button part of your program? or are you
> executing it again?
It is a part of my program
well if it calls back to that class then ya you will get an error again becuase you catch the error it doesn't terminate your program
the thing that is annoying me is that in class DataAccess I have an error
in class CallObjectFomHere I use the DataAccess object
so what is the best way to do to avoid the list of errors to be generated as the object is not 100% ok
not sure if this is what your asking but if you don't want errors to print out well just catch it and leave it don't print anything that and fix your program to be more realiable ^.^
> not sure if this is what your asking but if you don't
> want errors to print out well just catch it and leave
> it don't print anything that and fix your program to
> be more realiable ^.^
oohhh nooo this not what I need , I must see all possible errors ,It's a matter of designing the good error exceptions
any way thank you I will try to have a deep look and search again to find the best way
yup try this site if your looking for error exceptions
http://mindprod.com/jgloss/exception.html
> yup try this site if your looking for error
> exceptions
> http://mindprod.com/jgloss/exception.html
thank you ,I will
> not sure if this is what your asking but if you don't
> want errors to print out well just catch it and leave
> it don't print anything that and fix your program to
> be more realiable ^.^
Now that's some good advice. Yep, let's just swallow all exceptions. Exceptions, who needs 'em?
Jeepers.
<avoid the list of errors to be generated as the object is not 100% ok
if you wan't to avoid and know its not 100% ok your prob going to have to lol>
> if you wan't to avoid and know its not 100% ok your
> prob going to have to lol
throw new ParseException();
~