How to force try/catch

mansour77

New member
Joined
Oct 18, 2006
Messages
1
Programming Experience
1-3
Hello Every Body:
I am looking for a way to force other developers to try/catch when using a function or a sub. For example, in Java I can add the "throws" reserverd word to the declaration of a method. Therefore forcing any developer to use try/catch when using this method. Otherwise the class will not compile. Here's an example (in java):
public void readFile() throws SomeException
{
...
}

How can I get the same in VB.net ??
 
You can't get the same in VB.net.
 
If you want to throw an exception in VB by simply adding the "Throw" keyword and adding "New Exception"

VB.NET:
Throw New Exception

However; you wouldn't really want a class written in VB to not compile unless you request certain parameters to be matched in the New sub routine.

http://msdn2.microsoft.com/en-us/library/system.exception.getbaseexception(VS.80).aspx
That is a link with an example of using the Throw statement and an example of Inheriting from the Exception Class and adding your own Exception.

Here is a good way to prevent your class from compiling under a condition that you don't want to exist.

VB.NET:
Public Class ExampleClass
     Inherits Exception
 
     Public Sub New(ByVal x as integer)
          If x < 0 then
               Throw New Exception("X cannot be less than zero")
               MyBase.Finalize()
               Return
          End If
     End Sub
End Class

That will throw an exception if you attempt to build a new ExampleClass Object with a parameter less than 0. You should also look into the IDisposable interface if your Class is using un-managed resources such as directx ect... There's probably a lot more to it as well but I don't have tons of knowledge on it but hope that helps.
 
This has nothing to do with the question asked, ImDaFrEaK. The request was if it is possible to force users of a class method to try-catch for specific execptions - similar to how an interface forces implementation of all interface members. (and your class example build with a -1 instance)
 
Ok, I still don't get it. LOL... I am just more neive than the question which means it's probably related to something I don't know anything about. The way I interpreted it was like so. I will look into that thread though and I appreciate it.
 
Say your method threw a WebException.. question asked was there way to force anyone using your method to HAVE TO do this:
VB.NET:
Try
  YourMethod()
Catch WebException
  '..
End Try
The reason I compared with interface is because if you have this:
VB.NET:
Public Interface Itest
  Public Sub method1()
  Public Sub method2()
End Interface
Implementing it you would HAVE TO do this:
VB.NET:
Public Class mytest
Implements Itest
  Public Sub thismethod() Implements Itest.method1()
    '..
  End Sub
  Public Sub othermethod() Implements Itest.method2()
    '..
  End Sub
End Class
...it's about being bound by contract you can't break... which there for Exceptions exist no functionality in .Net like it does in Java.
 
Back
Top