Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么是C++;相当于Java'的异常处理机制;s输入不匹配异常和IOException处理_Java_C++ - Fatal编程技术网

什么是C++;相当于Java'的异常处理机制;s输入不匹配异常和IOException处理

什么是C++;相当于Java'的异常处理机制;s输入不匹配异常和IOException处理,java,c++,Java,C++,这是我的Java解决方案。但是,我希望在C++程序中也能实现相同的异常处理机制。 import java.util.Scanner; import java.io.*; import java.util.InputMismatchException; /*: NAME: Integrated Solution Create a C++ or Java program for accepting an integer (n) and generate following point series

这是我的Java解决方案。但是,我希望在C++程序中也能实现相同的异常处理机制。
import java.util.Scanner;
import java.io.*;
import java.util.InputMismatchException;
/*: 
NAME: Integrated Solution
Create a C++ or Java program for accepting an integer (n) and generate following point series:
x: n integer points from 0 to (n-1)
y: y=x*x

Design an effective mechanism for plotting these with minimal or nil effort from the user.*/

public class Integrated_Solution
 {
  private static final String FILENAME = "122.py";
  public static void main (String[] args)
   {
    Scanner sc = new Scanner (System.in);
    int n = -1;
    boolean flag = false;
    System.out.println ("Enter value of n: ");
    while(flag==false)
     {
      try
       {
        n = sc.nextInt();               // Inputting range from user
        if (n >= 0)
         flag=true;
        else
         System.out.println("Enter a positive value.");
       }
      catch (InputMismatchException e) // Exception Handling For any mismatch types (float, string etc.)
       {
        System.out.println ("Enter a positive integer value. \n");
        sc.nextLine();
       }
     }
    try
     {
      FileWriter fw = new FileWriter (FILENAME); // Create a new file with the given filename
      fw.write("from pylab import linspace, plot, stem, show, title, xlabel, ylabel\n"+                     
               "n = " + n + "\n" +
               "x = linspace (0, n - 1, n)\n"+
               "figure (num = None, figsize = (16,8), dpi = 80, facecolor='w', edgecolor='k')\n"
               "title ('Plot for graph $f(x) = x^2$')\n" +
               "xlabel ('$x$')\n" +
               "ylabel ('$x^2$')\n" +
               "plot (x, x * x, 'b')\n"+    
               "stem (x, x * x, linefmt = 'g-', markerfont= 'bo')\n"+                                                       
               "show ()\n");            
      fw.close();
     }
    catch (IOException e)
     {
      System.out.println("Could not write file !!" + e.getMessage());
     }
    try
     {
      Runtime.getRuntime().exec ("python " + FILENAME); // Execute the created python file 
     }
    catch (IOException e)
     {
      System.out.println("Could not execute script. Call to runtime failed. " + e.getMessage ());
     } 
    sc.close(); 
   }
 }
#包括
#包括
#包括
使用名称空间std;
int main()
{
流输出文件;
outputFile.open(“122.py”);
cout>n;

outputFile您可以很容易地设置何时抛出哪些异常。最常见的情况是
std::istream::failbit
istream::badbit
(老实说
std::ios::badbit
std::ios::failbit
,它们是继承的):

intmain(){
int n;
std::cin.异常(std::istream::failbit | std::istream::badbit);
试一试{
标准:cin>>n;
}捕获(istream::失败){

STD::CUT,而Python脚本则与问题本身无关,因此不应该有<代码> Python 标签。对于等价物,没有。java和C++是两种非常不同的语言,它们对异常的处理非常不同。例如C++中,抛出异常非常昂贵。这就是为什么它很少用于验证或简单错误处理的原因。如果您想了解如何从
系统
函数中处理错误,我建议,例如..或平台特定信息的or。一些建议-如您所愿。在java代码中,您正在关闭
try
语句中的资源,这是不好,因为它们在代码引发异常时不会关闭。您应该在catch方法后添加finally语句,或者作为一种好的做法-如果某个对象实现了
AutoCloseable
Closeable
接口,则它可以在try with resources语句中使用,该语句关闭资源,即使在发生异常后也是如此已经抛出,即使它是未捕获的,更多信息在这里。
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main ()
 {
  ofstream outputFile;
  outputFile.open("122.py");    
  cout << "Enter the value of n : ";
  int n;
  cin >> n;
  outputFile << "from pylab import linspace, plot, stem, show, title, xlabel, ylabel, figure\n" <<                      
               "n = " << n << "\n" <<
               "x = linspace (0, n - 1, n)\n"<< 
               "figure (num = None, figsize = (16,8), dpi = 80, facecolor='w', edgecolor='k')\n" <<
               "title ('Plot for graph $f(x) = x^2$')\n" <<            
               "xlabel ('$x$')\n" <<
               "ylabel ('$x^2$')\n" <<
               "plot (x, x * x, 'b')\n"<<   
               "stem (x, x * x, linefmt = 'g-', markerfont= 'bo')\n" <<             
               "show ()\n";
  outputFile.close();
  system ("python 122.py");
 }
int main() {
  int n;
  std::cin.exceptions(std::istream::failbit | std::istream::badbit);
  try {
    std::cin >> n;
  } catch(istream::failure) {
    std::cout << "failed to read value" << std::endl;
  }
  return 0;
}