Newbie: Show that function might throw exception

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there any way of clearly showing to the programmer that an exception might
be thrown from a function? In C++ you can write:

void MyClass::MyFunction(...) throw (MyException);

Is there anything similar in C#?

Thanks in advance
 
I am not sure if I understand your question 100%, but an exception can be
thrown from any method. If you are asking how to do it you can do something
like this

if ( myValue != WhatItShouldBe )
throw new Exception(....) ;

You can also write custom exception classes that inherit Exception.

Amy
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Hi Amy and thanks for your reply,

No. What I meant was if there is a way for me, as a programmer that develops
libraries that other programmers can use, to show to the other programmers
that the function that they can use can throw an exception. For instance,
I've been trying out the MySQLDriverCS library to connect to a MySQL
database. When typing the name of a function of an object created which
belongs to that API, and then selecting "Go to definition" I will only get an
"Object browser" which shows the functions available, but in that (nor in
the documentation...) view I cannot see any information about whether the
function throws any exception that I might have to catch. When writing

void MyClass::MyFunction(...) throw (MyException);

in C++ "throw (MyException) is only a way of clearly showing to another
programmer what he / she (perhaps) have to deal with when it comes to
exceptions.
 
Amy L. said:
I am not sure if I understand your question 100%, but an exception can be
thrown from any method. If you are asking how to do it you can do something
like this

if ( myValue != WhatItShouldBe )
throw new Exception(....) ;

You can also write custom exception classes that inherit Exception.

Amy
 
I'm so sorry for my multiple posts, but every time I try to post something I
get an error message back so I tried to post the post again. Hope MS can fix
this problem.
 
I'm so sorry for my multiple posts, but every time I try to post something I
get an error message back so I tried to post the post again. Hope MS can fix
this problem.
 
I'm so sorry for my multiple posts, but every time I try to post something I
get an error message back so I tried to post the post again. Hope MS can fix
this problem.
 
Joachim... C# does not support checked exceptions. One approach is to
explicitly state the pre-conditions that result in an exception in the
method
summary. It is up to the user of the method to read the documentation
and
validate the preconditions. If the user does this, then the user should
not
need to catch the exception. If the exception is from an invariant
violation or
post condition you could add WithExceptions to the method name.

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Regards,
Jeff
Is there any way of clearly showing to the programmer that an exception
might be thrown from a function?<
 
Back
Top