473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

handle error in called class or...

I'm unsure how best to handle errors in the scenario below!

Say I have a class Employee that handles everything about employees.
The class has two properties "id" and "name", and on procedure
"create" that simply inserts a new employee into the database.

Any error I'd like to show to the user. But how do I make an error
that occurs in the class display in the code that calls the class?

Should I have try catch in both the class (wrapping the ado code) and
in the calling code (wrapping the Employee.Create)?
Nov 19 '05 #1
7 1248
Hi

I assume you are talking about an ASP.NET app.

So See http://support.microsoft.com/default...b;en-us;306355

Regards,
Daniel Roth
MCSD.NET

hansiman wrote:
I'm unsure how best to handle errors in the scenario below!

Say I have a class Employee that handles everything about employees.
The class has two properties "id" and "name", and on procedure
"create" that simply inserts a new employee into the database.

Any error I'd like to show to the user. But how do I make an error
that occurs in the class display in the code that calls the class?

Should I have try catch in both the class (wrapping the ado code) and
in the calling code (wrapping the Employee.Create)?


Nov 19 '05 #2
Catching exceptions in both places makes sense if you want to supply your
own messages. In the class, when you get the exception, you know exact place
in the code when it occurs and can make a message like "Can't insert the new
employee into the database" or "Can't open the database". In the calling
class you catch this custom-made message and communicate it to the user.

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:fi********************************@4ax.com...
I'm unsure how best to handle errors in the scenario below!

Say I have a class Employee that handles everything about employees.
The class has two properties "id" and "name", and on procedure
"create" that simply inserts a new employee into the database.

Any error I'd like to show to the user. But how do I make an error
that occurs in the class display in the code that calls the class?

Should I have try catch in both the class (wrapping the ado code) and
in the calling code (wrapping the Employee.Create)?

Nov 19 '05 #3
I see. thanks for you answer.

So in my class I would:

class x

private ...
properties...

public sub create()
try
insert into database
catch ex as exception
log error
end try
end sub
end class

and in my code I would:

dim ox as new x
ox.name= "new name"
try
ox.create()
catch ex as exception
display error message
end try

or must I access the exception in my code differently?
/M
On Mon, 30 May 2005 11:15:37 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
Catching exceptions in both places makes sense if you want to supply your
own messages. In the class, when you get the exception, you know exact place
in the code when it occurs and can make a message like "Can't insert the new
employee into the database" or "Can't open the database". In the calling
class you catch this custom-made message and communicate it to the user.

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:fi********************************@4ax.com.. .
I'm unsure how best to handle errors in the scenario below!

Say I have a class Employee that handles everything about employees.
The class has two properties "id" and "name", and on procedure
"create" that simply inserts a new employee into the database.

Any error I'd like to show to the user. But how do I make an error
that occurs in the class display in the code that calls the class?

Should I have try catch in both the class (wrapping the ado code) and
in the calling code (wrapping the Employee.Create)?


Nov 19 '05 #4
The only comment is you should make sure the exception you catch in the
class will get to the caller. This means you have to throw your own
exception in the catch block in the class.

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:81********************************@4ax.com...
I see. thanks for you answer.

So in my class I would:

class x

private ...
properties...

public sub create()
try
insert into database
catch ex as exception
log error
end try
end sub
end class

and in my code I would:

dim ox as new x
ox.name= "new name"
try
ox.create()
catch ex as exception
display error message
end try

or must I access the exception in my code differently?
/M
On Mon, 30 May 2005 11:15:37 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
Catching exceptions in both places makes sense if you want to supply your
own messages. In the class, when you get the exception, you know exact placein the code when it occurs and can make a message like "Can't insert the newemployee into the database" or "Can't open the database". In the calling
class you catch this custom-made message and communicate it to the user.

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:fi********************************@4ax.com.. .
I'm unsure how best to handle errors in the scenario below!

Say I have a class Employee that handles everything about employees.
The class has two properties "id" and "name", and on procedure
"create" that simply inserts a new employee into the database.

Any error I'd like to show to the user. But how do I make an error
that occurs in the class display in the code that calls the class?

Should I have try catch in both the class (wrapping the ado code) and
in the calling code (wrapping the Employee.Create)?

Nov 19 '05 #5
So after catching and logging the original exception in the class I
should throw a new error that then would be catchable in the calling
try catch block?

On Mon, 30 May 2005 15:40:56 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
The only comment is you should make sure the exception you catch in the
class will get to the caller. This means you have to throw your own
exception in the catch block in the class.

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:81********************************@4ax.com.. .
I see. thanks for you answer.

So in my class I would:

class x

private ...
properties...

public sub create()
try
insert into database
catch ex as exception
log error
end try
end sub
end class

and in my code I would:

dim ox as new x
ox.name= "new name"
try
ox.create()
catch ex as exception
display error message
end try

or must I access the exception in my code differently?
/M
On Mon, 30 May 2005 11:15:37 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
>Catching exceptions in both places makes sense if you want to supply your
>own messages. In the class, when you get the exception, you know exactplace >in the code when it occurs and can make a message like "Can't insert thenew >employee into the database" or "Can't open the database". In the calling
>class you catch this custom-made message and communicate it to the user.
>
>Eliyahu
>
>"hansiman" <ha***@hotmail.com> wrote in message
>news:fi********************************@4ax.com.. .
>> I'm unsure how best to handle errors in the scenario below!
>>
>> Say I have a class Employee that handles everything about employees.
>> The class has two properties "id" and "name", and on procedure
>> "create" that simply inserts a new employee into the database.
>>
>> Any error I'd like to show to the user. But how do I make an error
>> that occurs in the class display in the code that calls the class?
>>
>> Should I have try catch in both the class (wrapping the ado code) and
>> in the calling code (wrapping the Employee.Create)?
>>
>>
>


Nov 19 '05 #6
yes

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:f3********************************@4ax.com...
So after catching and logging the original exception in the class I
should throw a new error that then would be catchable in the calling
try catch block?

On Mon, 30 May 2005 15:40:56 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
The only comment is you should make sure the exception you catch in the
class will get to the caller. This means you have to throw your own
exception in the catch block in the class.

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:81********************************@4ax.com.. .
I see. thanks for you answer.

So in my class I would:

class x

private ...
properties...

public sub create()
try
insert into database
catch ex as exception
log error
end try
end sub
end class

and in my code I would:

dim ox as new x
ox.name= "new name"
try
ox.create()
catch ex as exception
display error message
end try

or must I access the exception in my code differently?
/M
On Mon, 30 May 2005 11:15:37 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:

>Catching exceptions in both places makes sense if you want to supply your >own messages. In the class, when you get the exception, you know exact

place
>in the code when it occurs and can make a message like "Can't insert the
new
>employee into the database" or "Can't open the database". In the

calling >class you catch this custom-made message and communicate it to the user. >
>Eliyahu
>
>"hansiman" <ha***@hotmail.com> wrote in message
>news:fi********************************@4ax.com.. .
>> I'm unsure how best to handle errors in the scenario below!
>>
>> Say I have a class Employee that handles everything about employees.
>> The class has two properties "id" and "name", and on procedure
>> "create" that simply inserts a new employee into the database.
>>
>> Any error I'd like to show to the user. But how do I make an error
>> that occurs in the class display in the code that calls the class?
>>
>> Should I have try catch in both the class (wrapping the ado code) and >> in the calling code (wrapping the Employee.Create)?
>>
>>
>

Nov 19 '05 #7
:-) thanks

On Mon, 30 May 2005 16:03:59 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
yes

Eliyahu

"hansiman" <ha***@hotmail.com> wrote in message
news:f3********************************@4ax.com.. .
So after catching and logging the original exception in the class I
should throw a new error that then would be catchable in the calling
try catch block?

On Mon, 30 May 2005 15:40:56 +0200, "Eliyahu Goldin"
<re*************@monarchmed.com> wrote:
>The only comment is you should make sure the exception you catch in the
>class will get to the caller. This means you have to throw your own
>exception in the catch block in the class.
>
>Eliyahu
>
>"hansiman" <ha***@hotmail.com> wrote in message
>news:81********************************@4ax.com.. .
>> I see. thanks for you answer.
>>
>> So in my class I would:
>>
>> class x
>>
>> private ...
>> properties...
>>
>> public sub create()
>> try
>> insert into database
>> catch ex as exception
>> log error
>> end try
>> end sub
>> end class
>>
>> and in my code I would:
>>
>> dim ox as new x
>> ox.name= "new name"
>> try
>> ox.create()
>> catch ex as exception
>> display error message
>> end try
>>
>> or must I access the exception in my code differently?
>>
>>
>> /M
>>
>>
>> On Mon, 30 May 2005 11:15:37 +0200, "Eliyahu Goldin"
>> <re*************@monarchmed.com> wrote:
>>
>> >Catching exceptions in both places makes sense if you want to supplyyour >> >own messages. In the class, when you get the exception, you know exact
>place
>> >in the code when it occurs and can make a message like "Can't insertthe >new
>> >employee into the database" or "Can't open the database". In thecalling >> >class you catch this custom-made message and communicate it to theuser. >> >
>> >Eliyahu
>> >
>> >"hansiman" <ha***@hotmail.com> wrote in message
>> >news:fi********************************@4ax.com.. .
>> >> I'm unsure how best to handle errors in the scenario below!
>> >>
>> >> Say I have a class Employee that handles everything about employees.
>> >> The class has two properties "id" and "name", and on procedure
>> >> "create" that simply inserts a new employee into the database.
>> >>
>> >> Any error I'd like to show to the user. But how do I make an error
>> >> that occurs in the class display in the code that calls the class?
>> >>
>> >> Should I have try catch in both the class (wrapping the ado code)and >> >> in the calling code (wrapping the Employee.Create)?
>> >>
>> >>
>> >
>>
>


Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
7
by: Tony Johansson | last post by:
Hello!! Assume I have a handle body pattern with classes called Handle and Body. In the Body class I store one int value for example 7 or some other integer value. In the Handle class I have...
13
by: DraguVaso | last post by:
Hi, I have a Generic List, for instance: Public MyPersonnesDeContact As New System.Collections.Generic.List(Of clsPersonne) My clsPersonne raises some events, and I want to be able to handle...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
4
by: Terry | last post by:
There are a number of things about using unmanaged resources in Windows Forms programming that is unclear to me. In C++, if you loaded an icon resource using "ExtractIcon()", the resource was...
6
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
3
by: Peterwkc | last post by:
Hello all C++ expert programmer, I have a handle class which point to another class and use the pointer as object. I follow the code from C++ articles submited by someone in this forum....
4
by: lawrence k | last post by:
I'm working in PHP 4. Even if a parameter is mandatory, I nearly always give it a default value of "false". Then I print an error message if someone who is calling my function forgot the mandatory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.