Types of error: 1. Logical Error 2. Syntactic Error
Syntax errors: A syntax error is an error in the source code of a program.
Since computer programs must follow strict syntax to compile correctly, any aspects of the code that do not conform to the syntax of the programming language will produce a syntax error. Syntax errors are small grammatical mistakes, sometimes limited to a single character. For example, a missing semicolon at the end of a line or an extra bracket at the end of a function may produce a syntax error. Logic errors: A logic error (or logical error) is a ‘bug’ or mistake in a program’s source code that results in incorrect or unexpected behaviour. It is a type of runtime error that may simply produce the wrong output or may cause a program to crash while running. Many different types of programming mistakes can cause logic errors. Exception Handling • Try Block • Catch Block • Throw Block Try Block Syntax: try(expression); Throw Block Syntax: throw(type object) { --------------; --------------; //error message; } Catch Block Syntax: catch(type argument) { --------------; --------------; } Exception Handling steps (sequence) Syntax: try { ……………..; Throw exception; ……………..; ……………..; } Catch(type argument) { ……………..; ……………..; ……………..; ……………..; } #include<iostream.h> Using namespace std; Void main() { cout<<“you are In main function”; try { cout<<“Inside the try block”; } catch(int i) { cout<<“Caught an exception”; cout<<“Value of i is:”<<i; } cout<<“Out of each block”; } OUTPUT you are In main function Inside the try block Out of each block #include<iostream.h> Using naemspace std; Void myfunction(int i) { Cout<<“inside my function:”; If(i==1) throw i; } Void main() { cout<<“you are In main function”; try { cout<<“Inside the try block”; myfunction(0); myfunction(1); myfunction(2); } catch(int i) { cout<<“Caught an exception”; cout<<“Value of i is:”<<i; } cout<<“Out of each block”; } OUTPUT you are In main function Inside the try block Inside myfunction: Inside myfunction Caught an exception Value of i is: 1 Out of each block IN JAVA try { //stuff } catch(Exception e) { //do stuff } finally { //this is always run } Finally" is used in conjunction with a try/catch block. Anything inside of the "finally" clause will be executed regardless of if the code in the 'try' block throws an exception or not.