C# for Java Programmers

Exceptions

Exception handling permits you to execute code and handle error conditions prior to the program exiting. In many cases if you catch the exception you may be able to correct the problem and resume execution. In C# exception handling requires three code blocks: try, catch, and finally. It is very similar to exception handling in Java, except C# doesn t use the throws keyword.

As mentioned, among the most noticeable differences is the lack of the throws keyword. In Java, if a class throws an exception you indicate this with the throws keyword. If someone uses this class, then they have to handle the exceptions specified by the throws keyword. C# does not require this, which means that you re not forced to handle the exception.

Catching Exceptions

To catch an exception you would execute the code you want to execute in a try block, and follow this block with a catch block. The following section will give examples of how to catch exceptions.

The try, catch, finally Blocks

The core mechanism of exception handling in C# is use of the try, catch, and finally blocks. The code in which the exception is likely to occur must be encased in a try block followed by a catch block. Typically you would end with a finally block to perform any cleanup code that must occur regardless if an exception was thrown or not. This...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Concrete Blocks and Cement Blocks
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.