Author Topic: Bakit need pa natin ng Exceptions? Pwede bang kahit wala neto pag nagcocode ?  (Read 91 times)

0 Members and 1 Guest are viewing this topic.

Offline TDelight

  • Newbie
  • *
  • Posts: 40
  • Karma: +0/-0
    • View Profile
Bakit need pa natin ng Exceptions? Para saan ba eto?  Pwede bang kahit wala neto pag nagcocode ?

Techronnati | where technology never sleeps


Mountain View

Offline LadyProgrammer

  • Full Member
  • *
  • Posts: 118
  • Karma: +0/-0
    • View Profile
Kelangan natin eto for error trapping mechanism ng java code nyo. This is one of the best practices in programming. Even with other programming languages, there is code for error trapping.

Offline arpee

  • EquiBerks
  • Jr. Member
  • *
  • Posts: 81
  • Karma: +1/-0
    • View Profile
tama si LadyProgrammer.
 Let's say ganto:

...
Quote
int num1 = 0;
Scanner scan = new Scanner(System.in);
num1 = scan.nextInt();
...

bale, yung num1 magkakaroon ng input na integer galing sa user thru the console. Bale mag wowork po yan ng tama EXCEPT pag string ang ipasok ng user. Yan po ang reason kung bakit exception yung tawag sa mga errors sa java. Eto po yung formal definition ng Sun:

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

Now, pano magagawan ng paraan ang mga exceptions? Pwede gumamit ng throws or try-catch (or minsan try-catch-finally) na code blocks. Ang idi-discuss ko ngayon ay yung try-catch. Ganito po kung pano gumagana ang try-catch:

Quote
try {
statement1
statement2
...
}
catch (ExceptionCode var) {
statementI
statementII
...
}
System.out.println("hello po");

pag may error sa loob ng try block, hindi na nya i-e-execute yung next statement. yung catch statement kagad ang execute nya. Kaya sa example ko sa taas, pag nag error yung statement1, hindi na sya tutuloy kay statement2. Sa statementI na kagad sya. Then syempre statementII ang susunod. Isipin mo sya na pag nagkaproblem mga statements sa loob ni try, si catch ang sasalo sa problema. Si catch ang bahala. Ngayon kung wala naman naging problema si try, hindi na nya tatawagin si catch. lulukdawan na si catch kung walang naging problem si try. kaya sa example natin, kung walang error si try, diretso na sa 'System.out.println("hello po"); ang execution kasi nga lulukdawan na nya si catch dahil wala naman naging error.

Ngayon, kung dalawa yung catch blocks sa codes, ibig sabihin, depende kung anong uri ng error na naencounter ni try ang tatawagin. Kung gagamit ka ng eclipse, di ka mahihirapan masyado dito kasi mag sa-suggest nya sya kagad syo kung ano ang tamang exception na gagamitin sa catch block.

Now, eto po ang isang sample code na ginamitan ng try-catch. Kunwari may iniintay tayong input sa user na int. Pero string ang pinapasok nya, so mag e-error yun at ika-catch natin yun at magtatanong uli sa user hanggang sa tama ang ipasok nya:

Quote
import java.util.InputMismatchException;
import java.util.Scanner;

public class TryCatchSample {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int userInput = 0;
boolean correctInput = false;
do {
try{
System.out.println("Input an integer: ");
userInput = scan.nextInt();
correctInput = true;
} catch (InputMismatchException e) {
System.out.println("Invalid input!");
scan.nextLine();
}
} while(!correctInput);
System.out.println(correctInput + " is a valid integer. End of program");
}

}

Offline GBTalk

  • Full Member
  • *
  • Posts: 184
  • Karma: +0/-0
    • View Profile
It's a good programming practice na ginagamit natin eto.


 

Related Topics


Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.