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 ?

[2148 byte] By [linuxchilda] at [2007-11-27 11:42:03]
# 1

It can. why shouldn't it?

And whether it's the best way: the best way to do what?

CeciNEstPasUnProgrammeura at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 2

<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?>

mark07a at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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 ?

linuxchilda at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 4

> <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

linuxchilda at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 5

<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?>

mark07a at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 6

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 ?

linuxchilda at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 7

> <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

linuxchilda at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 8

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

mark07a at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 9

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

linuxchilda at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 10

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 ^.^

mark07a at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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

linuxchilda at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 12

yup try this site if your looking for error exceptions

http://mindprod.com/jgloss/exception.html

mark07a at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 13

> yup try this site if your looking for error

> exceptions

> http://mindprod.com/jgloss/exception.html

thank you ,I will

linuxchilda at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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.

warnerjaa at 2007-7-29 17:41:49 > top of Java-index,Java Essentials,Java Programming...
# 15

<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>

mark07a at 2007-7-29 17:41:53 > top of Java-index,Java Essentials,Java Programming...
# 16

> if you wan't to avoid and know its not 100% ok your

> prob going to have to lol

throw new ParseException();

~

yawmarka at 2007-7-29 17:41:53 > top of Java-index,Java Essentials,Java Programming...