VB.NET Throws Exception - vb.net

In Java you can mark a method as might throw an exception
e.g. 'public void foo() throws exception {'
This is useful as the developer can then see to putting that method in a try catch block.
Is there a way to do this in VB.NET so that Visual Studio (as in Eclipse) warns the developer to put this code in a try catch block?

There is no equivalent.
Its pretty stupid actually, because exceptions should be exceptionnal.
Just add it in the XML documentation.
Dont forget that the throws in Java forces the calling method to surround the call by a try catch bloc ...

Related

Catching unhandled exception in Intellij IDE [Kotlin]

Is there any way to catch unhandled exceptions in compile time in Intellij IDE (or some other way ?)
fun main() {
val list = listOf(1,2)
println(list.first())
}
As you can see list.first() throws an exception (if the list is empty), but I don't get any warning in the IDE to handle it properly. I already tried Csense plugin but unfortunately no luck. I'm not sure how much work it takes to achieve this stuff, I have looked at Intellij PSI element to know if is there any way to recursively trace the function call and to catch some unhandled exceptions, but it's not straight forward as I thought :)
It would be great if someone share the details if they already achieved this in their kotlin project.
Note:
The reason I asked this question is that I'm working on a project which uses kotlin arrow lib to make the code more functional style. So they use Either to handle errors explicitly instead of try/catch.

On passing exceptions from C++ to C#

In a webpage it's suggested that:
The moral of this story: don’t let exceptions propagate between managed and unmanaged code. The results won’t be pretty.
also suggested:
This is particularly pertinent when wrapping C++ methods. C++ exceptions will need to be mapped into an “out” parameter or a return value, so that managed code can know what error occurred, and (optionally) throw a managed exception to “propagate” the original C++ exception.
Based on the above statements, my understanding is that the safest (best?) way to pass exceptions from C++ to C# is by return value or output parameter. Is my understanding correct?

P/Inoke catching native exception

I am testing a C# .NET 4.0 application which interacts with an C++ unmanaged DLL through PInvoke and I'd like to catch any exceptions thrown by the dll.
I have the dll function wrapped in try/catch clause to handle the native exception, but when it gets fired it is ignored. Tried :
try { } catch {}
try {} catch (Exception)
try {} catch (SEHException)
try {} catch (Win32Exception)
to no avail
The only option that works is by setting the DllImport SetLastError property to true and
after calling the function checking with :
if (Marshal.GetLastWin32Error() !=0)
It is a satisfactory solution but I just wonder why the other options do not have any effect as well as wonder if if the native exception is fired by the unmanaged dll or by the Windows API itself since for example the exception is a :
System.ComponentModel.Win32Exception (0x80004005): There is not enough space on the disk
is that a notification from the Windows API itself ?
The simple explanation is that the native code just doesn't throw an exception. And yes, using GetLastWin32Error() is boiler plate for any Windows api function. Other code might use it too, although it isn't terribly common, anybody can call SetLastError() to set the thread's error code. C code otherwise never intentionally throws exceptions, the language doesn't support it.
The 0x80004005 error code is COM error code, E_FAIL. You don't use pinvoke to call COM functions, the CLR's support for COM interop takes care of it through an import library. You do get exceptions for COM errors, the CLR throws them when it sees that the COM method returned a failure code. It also uses IErrorInfo to get a better description for the error code, returned in the Exception.Message property.

How are try/catch blocks implemented?

If an exception occurs in a try block, how is execution transferred to the catch block? This is not a C#/Java/C++ question, I'm just wondering how it works internally.
this is not a c#/java/c++ question. How it works internally,how the line knows to go catch statement.
How this works internally makes this pretty much a c#/java/C++ question (because it will be implemented differently).
In Java, a try block installs itself into a special table (in the class file). When the JVM throws an exception, it looks at that table to see where the next catch or finally block to go to is.
When an exception occurs a special instruction is executed (usually called interrupt). This leads to executing a generic error handler that deduces which is the latest installed suitable exception handler. That handler is then executed.
There is a difference how exceptions are technically handled between natively compiled languages such as C++ and languages using byte-code being executed on a virtual machine such as Java or C#.
C++ compilers usually generate code that protocols the information needed for exception handling at runtime. A dedicated data structure is used to remember entrance/exit of try blocks and the associated exception handler. When an exception occurs, an interrupt is generated and control is passed to the OS which in turn inspects the call stack and determines which exception handler to call.
Further details are pretty well explained in the following article by Vishal Kochhar:
How a C++ compiler implements exception handling
In Java or .NET there is no need for the overhead of maintaining exception handling information as the runtime will be able to introspect the byte code to find the relevant exception handler. As a consequence, only exceptions that are actually thrown are causing an overhead.
It is basically parsing fundamentals of the language.
You can get all info at Here
it should work in all langues somewhat like this:
if (error_occured xy while doing things in try){
call_catch_part(error xy)
}
you could do the same in C even though there is no exception handling per se.
There you would use setjmp/longjmp unfortunately you then do not get the stack unwinding and have to handle all the nitty-gritty yourself.

Why do many languages treat Exception-objects as first-class citizens?

When we get an objects that is actually are exceptions, we can do with them anything that we can do with ordinar objects in our language. We can pass them as an argument, we can store them in some collection and, what is the worst, we can return them as a result from the methods!
So there is a possibility for someone to write smelly code like this:
public Exception doSomethingCritical()
{
Exception error = null;
try
{
...
}
catch (Exception e)
{
// maybe at least here is the logging of the error if we are lucky
error = e;
}
return error;
}
So the question is why is the concept of an Exception-object is a first-class citizen in many OO languages? Maybe it is better if we have only limited constructions in the language that is allowed for exception-objects (like throw).
The problem with treating exceptions as a special case, and allowing only limited functionality with exceptions is that ultimately somewhere every good program needs to do something with exceptions, whether it be a global error-handler or a limited item that appropriately deals with the exception. By making exceptions first class citizens, there are many benefits, such as:
Allowing subclassing of exceptions. This is invaluable for exception handling as code dealing with exceptions can narrow their scope and deal only with exceptions they know how to deal with. I'd go so far as to say that any non-global exception handling routine that is catching just "Exception" is probably doing it wrong.
Passing data along with exceptions. Exceptions aren't very useful if the only thing you know in your catch logic is that an exception occurred. Stack traces, messages and even custom data for certain exception types are invaluable in identifying and resolving the problem that caused the exception.
Creating error handling routines that use OOP themselves. If exceptions couldn't be passed as objects, you couldn't for instance have a library that deals with exceptions - well, at least not a well written one.
Besides all of that, there's no way to guard against bad exception handling like you posted above. Even if an exception wasn't a first class citizen, there's no way to stop a developer from eating any and all exceptions and carrying on their merry way (at least, without fundamentally breaking how we think of exceptions)
My opinion.
I think you are confusing two different things: the Exception and the Exception throwing.
The exception throwing is an out-of-band process that allows you to throw an object through a preferential, lateral channel. This object can be intercepted and handled through the Exception catching mechanism.
The Exception is just one object that you can (preferentially) throw via the Exception throwing out-of-band channel. Of course, this channel can have requisites for the interface of the object being thrown, requisites that are satisfied by the Exception class interface.
You are looking at the issue the other way around. Indeed you can do horrors, but there's nothing special about the Exception object, apart of being the preferred object that is thrown in the out-of-band channel.
I've never actually seen that example you've used. And I can't see how not allowing people to return exceptions will make a big difference to conceptually poor code - compare
public int doSomethingCritical()
{
int error = 0;
try
{
...
}
catch (Exception e)
{
// maybe at least here is the logging of the error if we are lucky
error = E_SOMETHINGBAD;
}
return error;
}
Whereas if you create a new kind of "thing" to be used for exceptions that isn't the same as an object, there is a design and learning overhead disadvantage.
How would you be able to inherit from the base exception class and derive your own exception class for your module if they were not first type citizens?
I don't see why I shouldn't be allowed to pass and return exceptions as normal method parameters. What if I were writing an Exception Handling library? What if I were writing a unit test assertion that compared exceptions?
Because life is a lot easier that way. Among other things, it means that the exception object can contain information that the program can use to correct the exceptional situation (perhaps with human intervention).
Abend is so 1960s.
I'm not sure the example you give is a strong enough case for not having exceptions as objects. After all you COULD do all kinds of "smelly" or "bad" things while programming. However thats precisely the reason we want good programmers. Just because I can do dsomething like :
def get_total
return nil
end
surely doesnt mean I should not allow nil as an instance of an object!