Computer Science(9608) notes and Resources
Exception: An exception is a problem that occurs during the execution of a program due to unforeseen circumstances such as an attempt to divide by zero while the program is running.
Exception: An exception is a problem that occurs during the execution of a program due to unforeseen circumstances such as an attempt to divide by zero while the program is running.
Exception handling: Exception handling is a mechanism in which a programming construct is ‘used to consistently trap, intercept and handle the error occurred during application execution
Advantages of Exception Handling
• Exception handling allows us to control the normal flow of the program by using exception handling in program.
• It throws an exception whenever a calling method encounters an error providing that the calling method takes care of that error.
• It also gives us the scope of organizing and differentiating between different errors types using a separate block of codes. This is done with the help of try-catch blocks.
• Provides a type-safe, integrated approach for coping with the unusual predictable problem that arises while executing program.
• Provide means to detect and report an “exceptional circumstance” so that appropriate action can be taken
• Easy identification of program code and error handling code (separating error handling code from “regular” code)
• Meaningful error reporting
• Grouping error types and error differentiation
• Increased reliability and better performance
Module Module1
Sub Main()
Dim num1, num2 As Integer
Console.WriteLine("Enter any number: ")
num1 = Console.ReadLine()
Console.WriteLine("Again enter any number: ")
num2 = Console.ReadLine()
Console.WriteLine("numberator/denominator = " & (num1 / num2))
Console.ReadLine()
End Sub
End Module
If the user enter value 6 for num1 and value 3 for num2, the answer is 2
If the user enter value 0 for num2, the answer is infinity
If a user enters a char or any string value., the program crashes
To solve any unforeseen event, the try catch block can be used
Module Module1
Sub Main()
Dim num1, num2 As Integer
Try
Console.WriteLine("Enter any number: ")
num1 = Console.ReadLine()
Console.WriteLine("Again enter any number: ")
num2 = Console.ReadLine()
Console.WriteLine("numberator/denominator = " & (num1 / num2))
Catch ex As Exception
Console.writeline("Invalid input")
End Try
Console.ReadLine()
End Sub
End Module
Now, the problem is resolved and the user is informed about the wrong input.
Importance of exception handling: To make sure that program always run smoothly and does not crash any event.
- CIE A-LEVEL COMPUTER SCIENCE ( 9608 )
- CIE A-LEVEL COMPUTER SCIENCE ( 9608 )
No comments:
Post a Comment
Comments here....