### opdracht 1 ### public class MainActivity extends AppCompatActivity { ... double num1, num2, div; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { ... div = num1 / num2; ... } catch (Exception e) { ... } } }); } }
Download hier het bestand.
### opdracht 2 ### public void onClick(View v) { try { ... if (num2 == 0) { addResult.setText("Delen door 0 niet toegestaan!"); } else { addResult.setText(Double.toString(div)); } ... } catch (Exception e) { ... } }
Download hier het bestand.
### oefneing 3 ### https://www.w3schools.com/java/java_try_catch.asp
Download hier het bestand.
### oefneing 4 ### public void onClick(View v) { try { num1 = Double.parseDouble(firstNumber.getText().toString()); num2 = Double.parseDouble(secondNumber.getText().toString()); div = num1 / num2; if (num2 == 0) { throw new ArithmeticException(); } else { addResult.setText(Double.toString(div)); } } catch (Exception e) { if (e instanceof NumberFormatException) { addResult.setText("Geen tekst a.u.b."); } else if (e instanceof ArithmeticException){ addResult.setText("Delen door 0 niet toegestaan!"); } else { addResult.setText(e.getMessage()); } } }
Download hier het bestand.