Error:-
- Errors are the mistake. An error may produced incorrect result or terminate the execution of the program.
- Errors are like logical or typing mistakes.
Error have two different types:-
1. Compile time error:- compile time error occur at compilation.This errors comes by typing mistakes.
Example:- System.out.println(Hello Java);
Error:-In above syntax double coutes are missing so this is called compile time error.
correct syntax:- System.out.println("Hello Java");
2. Runtime error:- Runtime error occur after successful compilation of program.This can happeen when logic is wrong(i.e result is mismatch).
Following are runtime error examples:-
- Dividing an integer by zero.
- passing a parameter to a method that is not in valid range or value.
- Attempting to use of negative size of array.
- Converting invalid string to a number.
Exception:-
- Exception is caused at runtime error in program.
- whenever java interpreter find an error such as dividing number by zero it creates an exception object and throws it to inform that an error has occurred.
- Java exception is an object.
Java can handled exception by using five keywords.
1.try 2.catch 3.throw 4.throws 5.finally
1.try:- The statement due to which error can occur must be stored in the try block.if any one statement generates an exception within a try block then it is thrown.
2.catch:- The user code can catch this exception using catch and handle it properly.
3.throw:- If the user wants to manually throw an exception then throw keyword is used.
4.throws:- Any exception that is generated out of method must be specify by the throws clause.
5.finally:- Any code that must be executed before method return is put in a finally block.
Example:-
public class ExceptionDemo {
public static void main(String[] args) {
int a = 15;
int b = 0;
try{
int c = a/b;
System.out.println("Result: "+c);
}
catch(ArithmeticException e){
System.out.println(e);
}finally{
System.out.println("always executes");
}
System.out.println("End");
}
}
- Dividing an integer by zero.
- passing a parameter to a method that is not in valid range or value.
- Attempting to use of negative size of array.
- Converting invalid string to a number.
Exception:-
- Exception is caused at runtime error in program.
- whenever java interpreter find an error such as dividing number by zero it creates an exception object and throws it to inform that an error has occurred.
- Java exception is an object.
Java can handled exception by using five keywords.
1.try 2.catch 3.throw 4.throws 5.finally
1.try:- The statement due to which error can occur must be stored in the try block.if any one statement generates an exception within a try block then it is thrown.
2.catch:- The user code can catch this exception using catch and handle it properly.
3.throw:- If the user wants to manually throw an exception then throw keyword is used.
4.throws:- Any exception that is generated out of method must be specify by the throws clause.
5.finally:- Any code that must be executed before method return is put in a finally block.
Example:-
public class ExceptionDemo {
public static void main(String[] args) {
int a = 15;
int b = 0;
try{
int c = a/b;
System.out.println("Result: "+c);
}
catch(ArithmeticException e){
System.out.println(e);
}finally{
System.out.println("always executes");
}
System.out.println("End");
}
}
No comments:
Post a Comment