467,075 Members | 951 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

Exception

Hello,

I have a function which communicates with a database:

Public Overloads Sub Create(ByVal name As String, _
ByVal comment As String)
Try
Dim dbBlog As Database = DatabaseFactory.CreateDatabase("MyDB")
Dim dbcBlog As DbCommand =
dbBlog.GetStoredProcCommand("CreateBlog")
dbBlog.AddInParameter(dbcBlog, "@BlogName", DbType.String, name)
dbBlog.AddInParameter(dbcBlog, "@BlogComment", DbType.String,
comment)
dbBlog.ExecuteNonQuery(dbcBlog)

Catch ex As SqlClient.SqlException
????????
End Try

End Sub

This is a function which is inside a library that will be compiled.
My question is how to let the user knows what is the exception?

1. Should I send the ex.string to console?
2. Should I return ex.String by making it a function instead of a sub?
3. Should I throw the exception inside my function? (I am not sure if
this is even necessary. See next question)

Catch ex As SqlClient.SqlException
throw(ex)
End Try

4. Should I move the Try, Catch block to the main user code like:

Try
Create("MyBlog", "This is my blog")
Catch ex As SqlClient.SqlException
????????
End Try

I really have no idea of how should I do this?
Remember the function is inside a class that will be compiled into a
DLL with the namespace Blogs.

Thanks,
Miguel

Sep 17 '07 #1
  • viewed: 1710
Share:
4 Replies
I would consider using first a global error handler. My personal preference
is to display a generic error message to the user (this is an ASP.NET
application so the web page is your only communication vector with the user)
and mailing the error details to the development team.

You can use a local try block to clean up resources and/or in the rare cases
where the user could really do something about the error.

--
Patrice

"shapper" <md*****@gmail.coma écrit dans le message de news:
11*********************@k79g2000hse.googlegroups.c om...
Hello,

I have a function which communicates with a database:

Public Overloads Sub Create(ByVal name As String, _
ByVal comment As String)
Try
Dim dbBlog As Database = DatabaseFactory.CreateDatabase("MyDB")
Dim dbcBlog As DbCommand =
dbBlog.GetStoredProcCommand("CreateBlog")
dbBlog.AddInParameter(dbcBlog, "@BlogName", DbType.String, name)
dbBlog.AddInParameter(dbcBlog, "@BlogComment", DbType.String,
comment)
dbBlog.ExecuteNonQuery(dbcBlog)

Catch ex As SqlClient.SqlException
????????
End Try

End Sub

This is a function which is inside a library that will be compiled.
My question is how to let the user knows what is the exception?

1. Should I send the ex.string to console?
2. Should I return ex.String by making it a function instead of a sub?
3. Should I throw the exception inside my function? (I am not sure if
this is even necessary. See next question)

Catch ex As SqlClient.SqlException
throw(ex)
End Try

4. Should I move the Try, Catch block to the main user code like:

Try
Create("MyBlog", "This is my blog")
Catch ex As SqlClient.SqlException
????????
End Try

I really have no idea of how should I do this?
Remember the function is inside a class that will be compiled into a
DLL with the namespace Blogs.

Thanks,
Miguel

Sep 17 '07 #2
On Sep 17, 1:30 pm, "Patrice" <http://www.chez.com/scribe/wrote:
I would consider using first a global error handler. My personal preference
is to display a generic error message to the user (this is an ASP.NET
application so the web page is your only communication vector with the user)
and mailing the error details to the development team.

You can use a local try block to clean up resources and/or in the rare cases
where the user could really do something about the error.

--
Patrice

"shapper" <mdmo...@gmail.coma écrit dans le message de news:
1190030292.258375.95...@k79g2000hse.googlegroups.c om...
Hello,
I have a function which communicates with a database:
Public Overloads Sub Create(ByVal name As String, _
ByVal comment As String)
Try
Dim dbBlog As Database = DatabaseFactory.CreateDatabase("MyDB")
Dim dbcBlog As DbCommand =
dbBlog.GetStoredProcCommand("CreateBlog")
dbBlog.AddInParameter(dbcBlog, "@BlogName", DbType.String, name)
dbBlog.AddInParameter(dbcBlog, "@BlogComment", DbType.String,
comment)
dbBlog.ExecuteNonQuery(dbcBlog)
Catch ex As SqlClient.SqlException
????????
End Try
End Sub
This is a function which is inside a library that will be compiled.
My question is how to let the user knows what is the exception?
1. Should I send the ex.string to console?
2. Should I return ex.String by making it a function instead of a sub?
3. Should I throw the exception inside my function? (I am not sure if
this is even necessary. See next question)
Catch ex As SqlClient.SqlException
throw(ex)
End Try
4. Should I move the Try, Catch block to the main user code like:
Try
Create("MyBlog", "This is my blog")
Catch ex As SqlClient.SqlException
????????
End Try
I really have no idea of how should I do this?
Remember the function is inside a class that will be compiled into a
DLL with the namespace Blogs.
Thanks,
Miguel
Hi,

you mean I can display my error as ASP.NET usually displays errors (In
those pages where text is in red)?
How can I do this? Does this has a name so I can look in Google?

Thanks,
Miguel

Sep 17 '07 #3
Try :
http://support.microsoft.com/kb/306355/en-us

and see the Application_Error event...

You'll then have a first point where to handle errors. You'll be able to add
then more local code (if you need to do sometying specific either for the
user or to clean up resources before letting the global error handler comes
into play).

--
Patrice

"shapper" <md*****@gmail.coma écrit dans le message de news:
11*********************@50g2000hsm.googlegroups.co m...
On Sep 17, 1:30 pm, "Patrice" <http://www.chez.com/scribe/wrote:
I would consider using first a global error handler. My personal
preference
is to display a generic error message to the user (this is an ASP.NET
application so the web page is your only communication vector with the
user)
and mailing the error details to the development team.

You can use a local try block to clean up resources and/or in the rare
cases
where the user could really do something about the error.

--
Patrice

"shapper" <mdmo...@gmail.coma écrit dans le message de news:
1190030292.258375.95...@k79g2000hse.googlegroups.c om...
Hello,
I have a function which communicates with a database:
Public Overloads Sub Create(ByVal name As String, _
ByVal comment As String)
Try
Dim dbBlog As Database = DatabaseFactory.CreateDatabase("MyDB")
Dim dbcBlog As DbCommand =
dbBlog.GetStoredProcCommand("CreateBlog")
dbBlog.AddInParameter(dbcBlog, "@BlogName", DbType.String, name)
dbBlog.AddInParameter(dbcBlog, "@BlogComment", DbType.String,
comment)
dbBlog.ExecuteNonQuery(dbcBlog)
Catch ex As SqlClient.SqlException
????????
End Try
End Sub
This is a function which is inside a library that will be compiled.
My question is how to let the user knows what is the exception?
1. Should I send the ex.string to console?
2. Should I return ex.String by making it a function instead of a sub?
3. Should I throw the exception inside my function? (I am not sure if
this is even necessary. See next question)
Catch ex As SqlClient.SqlException
throw(ex)
End Try
4. Should I move the Try, Catch block to the main user code like:
Try
Create("MyBlog", "This is my blog")
Catch ex As SqlClient.SqlException
????????
End Try
I really have no idea of how should I do this?
Remember the function is inside a class that will be compiled into a
DLL with the namespace Blogs.
Thanks,
Miguel
Hi,

you mean I can display my error as ASP.NET usually displays errors (In
those pages where text is in red)?
How can I do this? Does this has a name so I can look in Google?

Thanks,
Miguel
Sep 17 '07 #4
This is probably the best article on ExceptionHandling do and don'ts.

http://blogs.msdn.com/kcwalina/archi...16/396787.aspx

Don't forget, you can write your code like this

Try
//do stuff
Finally
//clean up stuff
End Try
You don't have to use a Catch.

If all you do is this

Try
'do stuff

Catch ex As SqlClient.SqlException
throw ''notice just a "throw" and not a "throw ex"
end try

then just use a try/finally block

......................

The one place you might consider
Catch ex As SqlClient.SqlException
throw new UserFriendlyDatabaseException ("The database is currently
down. Please try again later.");
End Try

In this scenario, you create a custom exception. And you are hiding details
from your user, and you are giving them a more generic exception.
Especially on a public website, where you don't want to expose what the
Server/Database name is to the public.

Dont' overdo this, or abuse it. Read the article above and bookmark it.


>
Catch ex As SqlClient.SqlException
throw(ex)
End Try

4. Should I move the Try, Catch block to the main user code like:

"shapper" <md*****@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
Hello,

I have a function which communicates with a database:

Public Overloads Sub Create(ByVal name As String, _
ByVal comment As String)
Try
Dim dbBlog As Database = DatabaseFactory.CreateDatabase("MyDB")
Dim dbcBlog As DbCommand =
dbBlog.GetStoredProcCommand("CreateBlog")
dbBlog.AddInParameter(dbcBlog, "@BlogName", DbType.String, name)
dbBlog.AddInParameter(dbcBlog, "@BlogComment", DbType.String,
comment)
dbBlog.ExecuteNonQuery(dbcBlog)

Catch ex As SqlClient.SqlException
????????
End Try

End Sub

This is a function which is inside a library that will be compiled.
My question is how to let the user knows what is the exception?

1. Should I send the ex.string to console?
2. Should I return ex.String by making it a function instead of a sub?
3. Should I throw the exception inside my function? (I am not sure if
this is even necessary. See next question)

Catch ex As SqlClient.SqlException
throw(ex)
End Try

4. Should I move the Try, Catch block to the main user code like:

Try
Create("MyBlog", "This is my blog")
Catch ex As SqlClient.SqlException
????????
End Try

I really have no idea of how should I do this?
Remember the function is inside a class that will be compiled into a
DLL with the namespace Blogs.

Thanks,
Miguel

Sep 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Nicolas Fleury | last post: by
1 post views Thread by Old Wolf | last post: by
11 posts views Thread by Master of C++ | last post: by
4 posts views Thread by maricel | last post: by
44 posts views Thread by craig | last post: by
40 posts views Thread by Kevin Yu | last post: by
6 posts views Thread by Vadivel Kumar | last post: by
3 posts views Thread by JohnDeHope3 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.