Int main()
{ double a, b;
std::cout << "Введите a и b через пробел: ";
std::cin >> a >> b;
try
{ withException( a, b );
}
catch ( myException &e )
{ std::cout << e.what() << std::endl;
}
////////////////////////////
int ret = returnErrorCode( a, b );
if ( ret != 0 )
{ std::cout << "Erorr in returnErrorCode()" << std::endl;
}
////////////////////////////
int err = 0;
withSetErrorCode( a, b, &err );
if ( err != 0 )
{ std::cout << "Erorr in withSetErrorCode()" << std::endl;
}
//////////////////////////////
return 0;
}
void withException( double a, double b)
{ if ( b == 0 )
throw myException( "Exception at withException()" );
else std::cout << "a/b = " << (a / b) << std::endl;
}
int returnErrorCode(double a, double b)
{ if ( b == 0 ) return -1;
else
std::cout << "a/b = " << (a / b) << std::endl;
return 0;
}
void withSetErrorCode( double a, double b, int *err )
{ if ( b == 0 ) { *err = -1; return;}
else { *err = 0;
std::cout << "a/b = " << (a / b) << std::endl;
}
}
Пример 7
package primer;
import java.util.*;
public class Primer
{ static int MyDel(int x, int y)
{ return x / y;
}
public static void main(String[] args)
{ boolean fl; int x,y,result;
Scanner in;
do
{ fl = false;
try
{ in = new Scanner(System.in);
System.out.print("Введите x: ");
x = in.nextInt();
System.out.print("Введите y: ");
y = in.nextInt();
result = MyDel(x, y);
System.out.println("Результат: " + result);
}
// Обрабатываем исключение возникающее при делении на ноль
catch (ArithmeticException e)
{ System.out.println("Ошибка: " + e); fl = true;
}
// Обрабатываем исключение при неккоректном вводе числа в консоль
catch (InputMismatchException e)
{ System.out.println("Это НЕ число!!!" + e); fl = true;
}
} while(fl);
}
}
Пример 8
package primer;
import java.util.*;
public class Primer
{ static double MyDel(double x, double y)
{ if (y == 0) throw new ArithmeticException();
else return x / y;
}
public static void main(String[] args)
{ boolean fl; double x,y,result;
Scanner in;
do
{ fl = false;
try
{ in = new Scanner(System.in);
System.out.print("Введите x: ");
x = in.nextDouble();
System.out.print("Введите y: ");
y = in.nextDouble();
result = MyDel(x, y);
System.out.println("Результат: " + result);
}
// Обрабатываем исключение возникающее при делении на ноль
catch (ArithmeticException e)
{ System.out.println("Ошибка: " + e); fl = true;
}
// Обрабатываем исключение при неккоректном вводе числа в консоль
catch (InputMismatchException e)
{ System.out.println("Это НЕ число!!!" + e); fl = true;
}
} while(fl);
}
}
Пример 9 (вариант 1)
package primer;
import java.io.*;
public class Primer
{ public static void main(String[] args)
{ try {
String str;
BufferedReader r = new BufferedReader(new FileReader("E:\\info.txt"));
while ((str = r.readLine())!= null)
System.out.println(str);
}
catch (IOException ex) {
System.out.println("Ошибка: " + ex);
}
}
}
Пример 9 (вариант 2)
package primer;
import java.io.*;
public class Primer
{ public static void main(String[] args) throws IOException
{ String str;
BufferedReader r = new BufferedReader(new FileReader("E:\\info2.txt"));
while ((str = r.readLine())!= null)
System.out.println(str);
}
}
Пример 10
package primer;
class MyException extends Exception
{ double min,max;
String error;
MyException(double x, double y, String str)
{ min = x; max = y; error = str;}
@Override
public String toString()
{ return "Ошибка(" + error + "): попадание в диапазон ["+min+", "+max+"]";}
}
class Primer
{ static double MyLog(double x)throws MyException
{ if (x < 0 || x > 1) return Math.log(x*(x-1));
else throw new MyException(0,1,"неверный аргумент");
}
public static void main(String[] args)
{ double x = -1.2, y = 1.2, z = 0.5;
try
{ System.out.println("ln("+x+") = " + MyLog(x));
System.out.println("ln("+y+") = " + MyLog(y));
System.out.println("ln("+z+") = " + MyLog(z));
}
catch (MyException e)
{ System.out.println(e);
}
}
}
Пример 11
using System;
namespace ConsoleApplication1
{ class Program
{ static int MyDel(int x, int y)
{ return x / y;
}
static void Main(string[] args)
{ bool fl; int x,y,result;
do
{ fl = false;
try
{ Console.Write("Введите x: ");
x = int.Parse(Console.ReadLine());
Console.Write("Введите y: ");
y = int.Parse(Console.ReadLine());
result = MyDel(x, y);
Console.WriteLine("Результат: " + result);
}
// Обрабатываем исключение возникающее при делении на ноль
catch (DivideByZeroException e)
{ Console.WriteLine("Ошибка: " + e.Message); fl = true;
}
// Обрабатываем исключение при неккоректном вводе числа в консоль
catch (FormatException e)
{ Console.WriteLine("Это НЕ число!!!" + e.Message); fl = true;
}
} while(fl);
Console.ReadLine();
}
}
}
Пример 12
using System;
class Person
{ private string name;
private int age;
public void set_Name(string name)
{ this.name = name; }
public string get_Name() { return name; }
public void set_Age(int age)
{ if (age < 18)
throw new Exception("Лицам до 18 регистрация запрещена");
else this.age = age;
}
public int get_Age() { return age; }
}
class Program
{ static void Main(string[] args)
{ try
{ Person p = new Person();
p.set_Age(17);
}
catch (Exception ex)
{ Console.WriteLine("Ошибка: " + ex.Message);
Console.WriteLine("Метод: " + ex.TargetSite);
}
Console.ReadLine();
}
}
Пример 13
using System;
class Person
{ private string name;
private int age;
public void set_Name(string name)
{ this.name = name; }
public string get_Name() { return name; }
public void set_Age(int age)
{ if (age < 18)
throw new PersonException("Лицам до 18 регистрация запрещена");
else this.age = age;
}
public int get_Age() { return age; }
}
class PersonException : Exception
{ public PersonException(string message)
: base(message)
{ }
}
class Program
{ static void Main(string[] args)
{ try
{ Person p = new Person();
p.set_Age(17);
}
catch (PersonException ex)
{ Console.WriteLine("Ошибка: " + ex);
//Console.WriteLine("Ошибка: " + ex.ToString());
}
Console.ReadLine();
}
}