Skip to content

Commit e834f52

Browse files
committed
Exception Handling Code snipppets
1 parent 6148420 commit e834f52

19 files changed

+214
-0
lines changed

ExceptionHandling/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

ExceptionHandling/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ExceptionHandling</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cdac;
2+
3+
import java.io.FileReader;
4+
5+
public class CheckedExceptions {
6+
7+
public static void main(String[] args) {
8+
//this exception checked at compile time
9+
//handle this with try catch to compile the program
10+
FileReader filereader = new FileReader("FilePath");
11+
12+
13+
14+
}
15+
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.cdac;
2+
3+
public class UnCheckedExceptions {
4+
5+
public static void main(String[] args) {
6+
numberFormatException();
7+
divideByZero();
8+
nullPointerException();
9+
stringIndexOutOfBoundException();
10+
classCastException();
11+
}
12+
13+
public static void numberFormatException(){
14+
String str = "ten";
15+
16+
//this code will throw NumberFormatException
17+
//this is runtime exception
18+
//this exception checked at runtime only
19+
//use try catch to handle this exception
20+
int i = Integer.parseInt(str);
21+
}
22+
23+
public static void divideByZero(){
24+
//the below code will throw ArithmeticException
25+
//divide by 0 error
26+
//use try catch to handle this exception
27+
int a = 10/0;
28+
}
29+
30+
public static void nullPointerException(){
31+
String firstName = null;
32+
int length = firstName.length();
33+
System.out.println("Length of the first name is : "+length);
34+
}
35+
36+
public static void stringIndexOutOfBoundException(){
37+
String name = "Core Java";
38+
char charAtIndex100 = name.charAt(100);
39+
System.out.println("Char at index 100 is : "+charAtIndex100);
40+
}
41+
42+
public static void classCastException(){
43+
Object o = new Integer(10);
44+
String sObj = (String) o;
45+
System.out.println(sObj);
46+
}
47+
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.cdac.customexception;
2+
3+
public class Student implements Comparable<Student>{
4+
5+
private int rollNo;
6+
private String name;
7+
8+
public Student(){
9+
rollNo = -1;
10+
name = "";
11+
}
12+
13+
public Student(int rollNo, String name) {
14+
super();
15+
this.rollNo = rollNo;
16+
this.name = name;
17+
}
18+
19+
public int getRollNo() {
20+
return rollNo;
21+
}
22+
23+
public void setRollNo(int rollNo) {
24+
this.rollNo = rollNo;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return rollNo+" "+name;
38+
}
39+
40+
@Override
41+
public int compareTo(Student o) {
42+
return this.rollNo - o.rollNo;
43+
44+
//DECREASING ORDER BY ROLL NO
45+
// return o.rollNo - this.rollNo ;
46+
}
47+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.cdac.customexception;
2+
3+
public class StudentDB {
4+
Student[] students;
5+
6+
public StudentDB() {
7+
students = new Student[3];
8+
Student s1 = new Student(1,"Name1");
9+
Student s2 = new Student(2,"Name2");
10+
Student s3 = new Student(3,"Name3");
11+
students[0] = s1;
12+
students[1] = s2;
13+
students[2] = s3;
14+
}
15+
16+
public Student getStudent(int rollNo) throws StudentNotFoundException{
17+
for(int i=0;i<students.length;i++)
18+
if(rollNo == students[i].getRollNo())
19+
return students[i];
20+
throw new StudentNotFoundException("Student Record not found");
21+
}
22+
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cdac.customexception;
2+
3+
public class StudentMain {
4+
5+
public static void main(String[] args) {
6+
StudentDB database = new StudentDB();
7+
try {
8+
//there is no record for rollno 6
9+
//so the code will throw exception
10+
Student s = database.getStudent(6);
11+
System.out.println("Student name is :"+s.getName());
12+
} catch (StudentNotFoundException e) {
13+
System.out.println(e.getMessage());
14+
}
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.cdac.customexception;
2+
3+
public class StudentNotFoundException extends Exception{
4+
public StudentNotFoundException(String message){
5+
super(message);
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.cdac.errors;
2+
3+
public class OutOfMemoryError {
4+
public static void main(String[] args) {
5+
//this code will throw OutOfMemoryError
6+
//VM does not have enough space to create the objects
7+
Integer intArray[] = new Integer[Integer.MAX_VALUE];
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.cdac.errors;
2+
3+
public class StackOverflowError {
4+
5+
public static void main(String[] args) {
6+
//this code will produce stackoverflow error
7+
//calling main method inside main method
8+
main(args);
9+
10+
//calling any method without any base condition
11+
//will lead to stack overflow error
12+
}
13+
14+
}

0 commit comments

Comments
 (0)