Exceptions

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 7676 Views - Last Post: 22 August 2011 - 06:42 AM Rate Topic: -----

#1 unknownmaster80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 11

Exceptions

Posted 18 August 2011 - 04:54 AM

Can anyone tell me an example of all the exceptions.
i did search online like eg. ArithmeticException and DivideByZeroException. but it gave me the same results and both means that if an integer is divided by zero. It's like no difference at all. It's for my exam tomorrow so can someone give me an example so that i can understand the exceptions better? Thank you. :)

Common Visual Basic .NET Exceptions
ArithmeticException
ArrayTypeMismatchException
ArgumentException
DivideByZeroException
IndexOutOfRangeException
InvalidCastException
NullReferenceException
OutOfMemoryException
OverFlowException
SQLException

Is This A Good Question/Topic? 0
  • +

Replies To: Exceptions

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Exceptions

Posted 18 August 2011 - 05:09 AM

The names of those exception classes are themselves explaining what they mean.
DivideByZeroException is a sub class of ArithmeticException, which means every DivideByZero Exception is also Arithmetic Exception but not viceversa.

EDIT:
ArithmeticException: thrown for errors in an arithmetic, casting, or conversion operation
ArrayTypeMismatchException: thrown when an attempt is made to store an element of the wrong type within an array.
ArgumentException:thrown when one of the arguments provided to a method is not valid
DivideByZeroException: thrown when there is an attempt to divide an integral or decimal value by zero
IndexOutOfRangeException: thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array.
InvalidCastException: thrown for invalid casting or explicit conversion
NullReferenceException: thrown when there is an attempt to dereference a null object reference.
OutOfMemoryException: thrown when there is not enough memory to continue the execution of a program
OverFlowException: thrown when an arithmetic, casting, or conversion operation in a checked context results in an overflow
SQLException: thrown when SQL Server returns a warning or error.

This post has been edited by smohd: 18 August 2011 - 05:20 AM

Was This Post Helpful? 1
  • +
  • -

#3 unknownmaster80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 11

Re: Exceptions

Posted 18 August 2011 - 06:14 AM

View Postsmohd, on 18 August 2011 - 05:09 AM, said:

The names of those exception classes are themselves explaining what they mean.
DivideByZeroException is a sub class of ArithmeticException, which means every DivideByZero Exception is also Arithmetic Exception but not viceversa.

EDIT:
ArithmeticException: thrown for errors in an arithmetic, casting, or conversion operation
ArrayTypeMismatchException: thrown when an attempt is made to store an element of the wrong type within an array.
ArgumentException:thrown when one of the arguments provided to a method is not valid
DivideByZeroException: thrown when there is an attempt to divide an integral or decimal value by zero
IndexOutOfRangeException: thrown when an attempt is made to access an element of an array with an index that is outside the bounds of the array.
InvalidCastException: thrown for invalid casting or explicit conversion
NullReferenceException: thrown when there is an attempt to dereference a null object reference.
OutOfMemoryException: thrown when there is not enough memory to continue the execution of a program
OverFlowException: thrown when an arithmetic, casting, or conversion operation in a checked context results in an overflow
SQLException: thrown when SQL Server returns a warning or error.


thanks a lot for the help but all your answers is just copy and paste from google. i need to understand the exception with examples and not just what is the exception is all about...
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Exceptions

Posted 18 August 2011 - 06:25 AM

Have you researched what the MSDN Documentation say about each of those exceptions?
Was This Post Helpful? 0
  • +
  • -

#5 unknownmaster80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 11

Re: Exceptions

Posted 18 August 2011 - 06:50 AM

View PostAdamSpeight2008, on 18 August 2011 - 06:25 AM, said:

Have you researched what the MSDN Documentation say about each of those exceptions?

I did research on it. but it don't have any examples so i couldn't understand it fully. :X
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4576
  • View blog
  • Posts: 8,018
  • Joined: 08-June 10

Re: Exceptions

Posted 18 August 2011 - 04:51 PM

If you looked them up on the MSDN, you'd see sample code. I don't know what more you want. smohd went all out and explained exactly what each is for. What more could you possibly need?
Was This Post Helpful? 0
  • +
  • -

#7 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Exceptions

Posted 18 August 2011 - 06:13 PM

Microsoft tried REALLY hard to make things self explanatory.

a = 5 / 0

Throws a DivideByZero exception because you tried to divide by zero.

CInt("hi")

Throws an InvalidCast Exception because you're trying to cast a string to an integer. (you should be using Integer.Parse instead of CInt regardless.)

Integer.Parse("hello")

Throws a numberformat exception, because hello is not a number.

        Dim a(3) As Integer
        a(4) = 7

Throws an indexoutofrange exception because the index of 4 isn't in the range of the array addressed (which goes from 0 to 3)

...what I'm trying to get to is that a lot of the exceptions are self-explanatory, and no one here is gonna spend the time explaining them all. If you have problems with a specific exception or list of exceptions and want to post it, that's fine, but I know I'm not gonna list em all.
Was This Post Helpful? 1
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4576
  • View blog
  • Posts: 8,018
  • Joined: 08-June 10

Re: Exceptions

Posted 18 August 2011 - 06:33 PM

Quote

If you have problems with a specific exception or list of exceptions and want to post it, that's fine, but I know I'm not gonna list em all.


We shouldn't be listing any. This post is blatantly homework, and we don't do anybody any good by handing out answers to people unwilling to extend a basic amount of effort to do any research. I wouldn't want to find that the next developer my company hired had gotten his way through his classes by getting a forum to do his work for him.
Was This Post Helpful? 1
  • +
  • -

#9 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Exceptions

Posted 18 August 2011 - 06:44 PM

It does sound homework-y I'll admit, I'd say change professors if yours is giving you inane pointless waste-of-time homework like that though.
Was This Post Helpful? 0
  • +
  • -

#10 unknownmaster80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 11

Re: Exceptions

Posted 21 August 2011 - 11:35 AM

View PostTarkenfire, on 18 August 2011 - 06:13 PM, said:

Microsoft tried REALLY hard to make things self explanatory.

a = 5 / 0

Throws a DivideByZero exception because you tried to divide by zero.

CInt("hi")

Throws an InvalidCast Exception because you're trying to cast a string to an integer. (you should be using Integer.Parse instead of CInt regardless.)

Integer.Parse("hello")

Throws a numberformat exception, because hello is not a number.

        Dim a(3) As Integer
        a(4) = 7

Throws an indexoutofrange exception because the index of 4 isn't in the range of the array addressed (which goes from 0 to 3)

...what I'm trying to get to is that a lot of the exceptions are self-explanatory, and no one here is gonna spend the time explaining them all. If you have problems with a specific exception or list of exceptions and want to post it, that's fine, but I know I'm not gonna list em all.


thanks a lot for the help even if it's not all the answers. cause at least all the examples that you have given was all easily understandable. thanks a million again! :)

View PostCurtis Rutland, on 18 August 2011 - 04:51 PM, said:

If you looked them up on the MSDN, you'd see sample code. I don't know what more you want. smohd went all out and explained exactly what each is for. What more could you possibly need?


thanks for the help but i could not find any on that day though. maybe because i am still a bit new to looking though the MSDN. :X
I did not blamed smohd for copying the answers from google plus it was not what i wanted to find. i did my research before. so if i could find the answers from google i wouldn't be asking a question from this forum for help already. thanks a lot anyways :)

View PostCurtis Rutland, on 18 August 2011 - 06:33 PM, said:

Quote

If you have problems with a specific exception or list of exceptions and want to post it, that's fine, but I know I'm not gonna list em all.


We shouldn't be listing any. This post is blatantly homework, and we don't do anybody any good by handing out answers to people unwilling to extend a basic amount of effort to do any research. I wouldn't want to find that the next developer my company hired had gotten his way through his classes by getting a forum to do his work for him.

If it was a homework I wouldn't have blindly fake my way though and gave excuses that it was for my test. what ever that i have stated in my question was all real and none was a lie. but it's ok cause maybe it might have sounded like a homework too though.. :X

This post has been edited by unknownmaster80: 21 August 2011 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Exceptions

Posted 21 August 2011 - 12:57 PM

Those are definitions of those classes according to MSDN Documentation, at least you should differentiate the Documentation and Google and it was fair enough to say I copied them from the VB.NET language documentation.
May be if you have a specific problem with those exception, like if you dont know how they are thrown or when some of them may take place, because example code will only show you how to catch or throw them(handle them)!

This post has been edited by smohd: 21 August 2011 - 12:59 PM

Was This Post Helpful? 1
  • +
  • -

#12 unknownmaster80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 11

Re: Exceptions

Posted 22 August 2011 - 02:09 AM

View Postsmohd, on 21 August 2011 - 12:57 PM, said:

Those are definitions of those classes according to MSDN Documentation, at least you should differentiate the Documentation and Google and it was fair enough to say I copied them from the VB.NET language documentation.
May be if you have a specific problem with those exception, like if you dont know how they are thrown or when some of them may take place, because example code will only show you how to catch or throw them(handle them)!


Ohh. Sorry for that then, cause I can also directly search it from google too. and really thanks for the help for explaining more about the exceptions to me too. but i think example codes will be more easily understandable to me though..
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Exceptions

Posted 22 August 2011 - 02:39 AM

The problem is that many of the exceptions are available in use and there is no specific example except you will have examples on how to handle them. For example when you look at IO examples you will see how IOException is handled, and when you work with SQL database you will see how SQLException is handled.

Exceptions are abnormal conditions that cause the program to stop, so what we are doing is to handle them and cause the program not to stack when they occur.

Example when you are trying to copy/save something to the disc and then it happens that there is no enough space to save that file, the system will stack and stops working. So in order to handle this so that will not affect your program, we put the code that do such action which may cause runtime exception in try block so that we can catch any problem and allow our program to work. Example:
 try
'code that may throw exception here
catch ex as Exception
'do something when exception happens
end try


For more look at Exception Handling Best Practices
Was This Post Helpful? 0
  • +
  • -

#14 unknownmaster80  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 11

Re: Exceptions

Posted 22 August 2011 - 04:23 AM

View Postsmohd, on 22 August 2011 - 02:39 AM, said:

The problem is that many of the exceptions are available in use and there is no specific example except you will have examples on how to handle them. For example when you look at IO examples you will see how IOException is handled, and when you work with SQL database you will see how SQLException is handled.

Exceptions are abnormal conditions that cause the program to stop, so what we are doing is to handle them and cause the program not to stack when they occur.

Example when you are trying to copy/save something to the disc and then it happens that there is no enough space to save that file, the system will stack and stops working. So in order to handle this so that will not affect your program, we put the code that do such action which may cause runtime exception in try block so that we can catch any problem and allow our program to work. Example:
 try
'code that may throw exception here
catch ex as Exception
'do something when exception happens
end try


For more look at Exception Handling Best Practices


Ohh. So you mean to to prevent the program from being affected, you would need those exceptions. but why do they have so many different kinds of exception? Why not just straight away throw Exception and not ArrayTypeException, DivideByZeroException, etc?
Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Exceptions

Posted 22 August 2011 - 06:18 AM

This helps to identify what exactly exception was thrown so that appropriate action can be taken. For example if I have a code that reads numbers from a file and then use them in arithmetic expression, assume they are all in one try block like:
Try
 'open file
 'read from file
 'cast those numbers to integer
 'do your calculation using those numbers
Catch ex As FileNotFoundException
            'do something if file not found
Catch ex As IOException
            'do something for other IO exceptions
Catch ex As InvalidCastException
            'do another thing if cast fail
Catch ex As DivideByZeroException
            'may be change number to 1
Catch ex As ArithmeticException
            'do another thing for other arithmetic exceptions
End Try

Now you see, you can decide what to do according to what exception happen, rather than having one huge exception class that handles all exceptions
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2