Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 297e41e

Browse files
rtomarrtomar
rtomar
authored and
rtomar
committed
Exception Handling Code
1 parent 9c56fd8 commit 297e41e

File tree

12 files changed

+262
-0
lines changed

12 files changed

+262
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.rt.exceptionexamples;
2+
3+
public class Employee {
4+
5+
6+
int id;
7+
String name;
8+
public Employee(int id, String name) {
9+
super();
10+
this.id = id;
11+
this.name = name;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
// TODO Auto-generated method stub
17+
return "Employee Id"+id+" Employee Name:"+name;
18+
}
19+
20+
21+
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.rt.exceptionexamples;
2+
3+
class Exception10 {
4+
// Through an exception out of the method.
5+
static void procA() {
6+
try {
7+
System.out.println("inside procA");
8+
throw new RuntimeException("demo");
9+
} finally {
10+
System.out.println("procA's finally");
11+
}
12+
}
13+
// Return from within a try block.
14+
static void procB() {
15+
try {
16+
System.out.println("inside procB");
17+
return ;
18+
} finally {
19+
System.out.println("procB's finally");
20+
}
21+
}
22+
static void procC() {
23+
try {
24+
System.out.println("inside procC");
25+
System.exit(0);
26+
} finally {
27+
System.out.println("procC's finally");
28+
}
29+
}
30+
public static void main(String args[]) {
31+
try {
32+
procA();
33+
} catch (Exception e) {
34+
System.out.println("Exception caught");
35+
}
36+
procB();
37+
procC();
38+
}
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.rt.exceptionexamples;
2+
3+
class MyException extends RuntimeException {
4+
private int detail;
5+
MyException(int a) {
6+
detail = a;
7+
}
8+
public String toString() {
9+
return "MyException[" + detail + "]should be less than 10";
10+
}
11+
}
12+
class Exception11 {
13+
static void compute(int a) {
14+
System.out.println("Called compute(" + a + ")");
15+
if(a > 10)
16+
throw new MyException(a);
17+
System.out.println("Normal exit");
18+
}
19+
public static void main(String args[]) {
20+
21+
compute(1);
22+
compute(20);
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.rt.exceptionexamples;
2+
3+
class Exception2 {
4+
public static void main(String args[]) {
5+
int d, a;
6+
7+
try { // monitor a block of code.
8+
d = 1;
9+
a = 42 / d;
10+
int a1[]=new int[10];
11+
a1[11]=21;
12+
System.out.println("This will not be printed.");
13+
//throw new IllegalAccessException();
14+
} catch (ArithmeticException e) { // catch divide-by-zero error
15+
System.out.println("Division by zero.");
16+
}
17+
catch(ArrayIndexOutOfBoundsException e)
18+
{
19+
System.out.println("Array error");
20+
}
21+
22+
catch(Exception o)
23+
{
24+
System.out.println("Super Exception");
25+
}
26+
27+
System.out.println("After catch statement.");
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.rt.exceptionexamples;
2+
3+
class Exception3 {
4+
public static void main(String args[]) {
5+
try {
6+
int a = args.length;
7+
System.out.println("a = " + a);
8+
int b = 42 / a;
9+
int c[] = { 1 };
10+
c[42] = 99;
11+
} catch(ArithmeticException e) {
12+
//System.out.println("Divide by 0: " + e.toStri);
13+
e.printStackTrace();
14+
15+
} catch(ArrayIndexOutOfBoundsException e) {
16+
System.out.println("Array index oob: " + e);
17+
}
18+
System.out.println("After try/catch blocks.");
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rt.exceptionexamples;
2+
3+
class Exception4 {
4+
public static void main(String args[]) {
5+
try {
6+
int a = 0;
7+
int b = 42 / a;
8+
} catch(ArithmeticException e) { // ERROR - unreachable
9+
System.out.println("This is never reached.");
10+
}
11+
catch(Exception e) {
12+
System.out.println("Generic Exception catch.");
13+
}
14+
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.rt.exceptionexamples;
2+
3+
class Exception5 {
4+
public static void main(String args[]) {
5+
try {
6+
int a = args.length;
7+
8+
int b = 42 / a;
9+
System.out.println("a = " + a);
10+
11+
if(a==1) a = a/(a-a);
12+
if(a==2) {
13+
int c[] = { 1 };
14+
c[42] = 99;
15+
}
16+
} catch(Exception e) {
17+
System.out.println("Array index Out-of-bounds: " + e);
18+
19+
}
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.rt.exceptionexamples;
2+
3+
class Exception6 {
4+
static void nesttry(int a) {
5+
try {
6+
if(a==1) a = a/(a-a);
7+
if(a==2) {
8+
int c[] = { 1 };
9+
c[42] = 99;
10+
}
11+
} catch(ArrayIndexOutOfBoundsException e) {
12+
System.out.println("Array index out-of-bounds: " + e);
13+
}
14+
}
15+
public static void main(String args[]) {
16+
try {
17+
int a = 2;
18+
int b = 42 / a;
19+
System.out.println("a = " + a);
20+
nesttry(a);
21+
} catch(ArithmeticException e) {
22+
System.out.println("Divide by 0: " + e);
23+
}
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rt.exceptionexamples;
2+
3+
import java.io.IOException;
4+
5+
class Exception7 {
6+
static void demoproc() throws IOException {
7+
throw new IOException();//("demo");
8+
9+
}
10+
11+
public static void main(String args[]) throws IOException {
12+
13+
demoproc();
14+
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.rt.exceptionexamples;
2+
3+
class Exception8 {
4+
static void throwOne() throws IllegalAccessException {
5+
System.out.println("Inside throwOne.");
6+
throw new IllegalAccessException("demo");
7+
//throw new ArithmeticException();
8+
}
9+
public static void main(String args[]) {
10+
try {
11+
throwOne();
12+
} catch (IllegalAccessException e) {
13+
// TODO Auto-generated catch block
14+
System.out.println("Got it"+e.getMessage());
15+
}
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /