0

How to make this code (use try and catch) more simple?

I have a code like this ; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package shapecollection; /** * * @author Achmad Yusuf Naution */ public class ShapeCollection extends Shape{ private int MAX_EL; private Shape[] arrShape; private int indeks; public ShapeCollection() { super(); MAX_EL = 10; indeks = 0; arr

12th Apr 2018, 1:35 PM
Dispssin SP
Dispssin SP - avatar
5 Antworten
+ 1
Thx
15th Apr 2018, 6:02 PM
Dispssin SP
Dispssin SP - avatar
0
The code is not complete, maybe you could post a link to it
12th Apr 2018, 3:09 PM
cyk
cyk - avatar
0
Sorry, i can write all code on the description: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package shapecollection; /** * * @author Achmad Yusuf Naution */ public class ShapeCollection extends Shape{ private int MAX_EL; private Shape[] arrShape; private int indeks; public ShapeCollection() { super(); MAX_EL = 10; indeks = 0; arrShape = new Shape[MAX_EL]; } public void addShape(Shape S) { arrShape[indeks] = S; indeks++; } public Shape getShape(int indeks) { return arrShape[indeks]; } public void display() { int i; Triangle a; Rectangle b; for (i=0;i<=indeks;i++) { try { if(getShape(i) == (Rectangle) getShape(i)) { b = (Rectangle) getShape(i); System.out.print(b.name); b.display(); } } catch(Exception e) { } try { if(getShape(i) == (Triangle) getShape(i)) { a = (Triangle) getShape(i); System.out.print(a.name); a.display(); } } catch(Exception e) { } } } public int getIndeks() { return indeks; } @Override protected double getArea() { return 0; } @Override protected double getCircumference() { return 0; } }
12th Apr 2018, 3:12 PM
Dispssin SP
Dispssin SP - avatar
0
Just use one try block. If the code you have in the different try blocks will produce different kinds of exceptions, you can have two catch blocks.
12th Apr 2018, 3:18 PM
cyk
cyk - avatar
12th Apr 2018, 3:22 PM
cyk
cyk - avatar